logo

Extra Block Types (EBT) - New Layout Builder experience❗

Extra Block Types (EBT) - styled, customizable block types: Slideshows, Tabs, Cards, Accordions and many others. Built-in settings for background, DOM Box, javascript plugins. Experience the future of layout building today.

Demo EBT modules Download EBT modules

❗Extra Paragraph Types (EPT) - New Paragraphs experience

Extra Paragraph Types (EPT) - analogical paragraph based set of modules.

Demo EPT modules Download EPT modules

Scroll

Drupal Commerce: programmatic output of the "Add to Cart" button

22/02/2025, by Anonymous (not verified)

The question is, I need to programmatically display the "Add to cart" button in the Views template. I'm doing it based on the example http://drupal.org/node/1232470#comment-4801356 and the documentation http://api.drupalcommerce.org/api/Drupal%20Commerce/sites!all!modules!commerce!modules!cart!commerce_cart.module/function/commerce_cart_add_to_cart_form/DC

like this:

 

<?php
    // build the line items for shopping cart
    $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;
   
    // need to create config for quantity
    $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);
   
    // we alter the submit form to use our special theme function
    // need to move this to configuration
    $addtocart_form['submit']['#theme'][] = 'vtcommerce_button_small';
   
    $variables['cart'] = render($addtocart_form);
   
?>

So the button displays correctly, but the widget for adding the quantity of products does not appear, even though $line_item->quantity = 1; and $qty = 1;.

The question is: why?