Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
How to print the add to cart button in a custom content type template?
The add to cart button shows up on the default product content type installed with Ubercart, but doesn't show on custom content types.
I can't find an example node template file either, and all examples on the web talk about how to print the add to cart button in Drupal 7.
Looking a the code in uc_product, I came up with this:
$form_object = new \Drupal\uc_product\Form\AddToCartForm($node->nid->getString());
$add_to_cart_form = \Drupal::formBuilder()->getForm($form_object, $node);
dsm($add_to_cart_form);
This gives me an object in $add_to_cart_form but what do I do with it? Is this the right way to print the add to cart button in D8 for ubercart?
Comments
Comment #2
David Fiaty CreditAttribution: David Fiaty commentedComment #3
TR CreditAttribution: TR commentedYou can't just add the add-to-cart form in a template to an arbitrary content type, that doesn't make sense. The content type has to be an Ubercart product content type, then the add-to-cart button will automatically show up on that content type along with all the other fields that are necessary for an Ubercart product like price, sku, etc.
To make a content type an Ubercart product content type, edit the content type at /admin/structure/types/manage/{name of content type}, choose the "Ubercart product settings" tab in the lower left of the page, select "Content type is a product", then save the content type.