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);


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?


David Fiaty created an issue. See original summary.

David Fiaty’s picture

Issue summary: View changes
TR’s picture

Status: Active » Fixed

You 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.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.