I have a site where I created a custom page to list products (can't use views module due to custom requirements for printing that list). On the list page for each product I have variation sku available. I want to show add to cart form with qty field on this custom list page. I have searched for a solution but couldn't find one for drupal 8. I know how to do it in drupal 7 but can someone help me with how we can have this in D8?
1 Answer
I got solution to the above problem. I used a drupal commerce service to load the form. Below is the code which I used, please let me know if it is an acceptable solution.
$commerceProduct = \Drupal::service('commerce_product.lazy_builders');
$cart_array = $commerceProduct->addToCartForm($product_id, 'teaser', true, $language);
This gave me a renderable array which I rendered using render service. Add to cart form is working fine.
-
Yes it worked for me. Thank you. Is there way to make this add to cart as an ajax calls? Any idea? Commented Feb 16, 2020 at 12:58