When anonymous users add a product to the cart it’s not working, but it works for authenticated users.

Following code is creating the form

  function mycustommodule_preprocess_commerce_product(array &$variables)
  // some code
  $service = \Drupal::service('commerce_product.lazy_builders');
  $variables['form_cart'] = $service->addToCartForm($product->id(), 
   'default', false, 'en');

and in the template(commerce-product.html.twig) we have :

{{ form_cart }}

The form is shown correctly but on submit to add to cart form nothing is added to cart when user is anonymous

  • Welcome to Drupal Answers :) Please note that a dump of a couple of lines of context-less code can't be considered a high quality question, and as we're building a repository of high quality questions, this can't live here. Please have a look through the help center to find out what makes a good question for this website, and if you still need help, use that information to post a much more detailed and answerable question. Many thanks – Clive Apr 15 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.