logo

Palette - 让它色彩缤纷🎨

Palette — 可视化页面构建器,无需专业设计背景。

在线演示 下载 Palette

滚动

Drupal Commerce: 程序化输出“添加到购物车”按钮

22/02/2025, by 匿名 (未验证)
论坛

问题是,我需要在Views模板中程序性地输出“添加到购物车”按钮。 我是按照 http://drupal.org/node/1232470#comment-4801356 的示例和文档 http://api.drupalcommerce.org/api/Drupal%20Commerce/sites!all!modules!commerce!modules!cart!commerce_cart.module/function/commerce_cart_add_to_cart_form/DC

这样做的:

 

<?php
    // 为购物车构建行项目
    $line_item = commerce_line_item_new($view['product']->type, $order_id = 0);
    $line_item->data['context']['product_ids'] = array($view['product']->product_id);
    $line_item->quantity = 1;
   
    // 需要为数量创建配置
    $qty = 1;
   
    $form_id = commerce_cart_add_to_cart_form_id(array($view['product']->product_id), $qty);
    $addtocart_form = drupal_get_form($form_id, $line_item);
   
    // 我们修改提交表单以使用我们的特殊主题函数
    // 需要将其移动到配置中
    $addtocart_form['submit']['#theme'][] = 'vtcommerce_button_small';
   
    $variables['cart'] = render($addtocart_form);
   
?>

所以按钮正常显示,但商品数量的控件没有出现,尽管 $line_item->quantity = 1; 和 $qty = 1; 。

问题是:为什么?