Hello,

What I'm trying to achieve is having a 'Choose option' or a 'None' as a add to cart select option and loading the variation fields only when the user chooses a variation. I more or less can achive this by using JS to disable the add to cart button and hide the fields, but would be more interesting to have it on the server side and not having to load images and text just the fields placeholders for the ajax response. I was trying to use the subscrition to the getDefaultVariation event but returning null in not an option. Any way to create a dummy variant to achieve this? or am I on the wrong track?

Thanks in advance.

Comments

joaoverissimo created an issue. See original summary.

pankaj1390’s picture

@joaoverissimo did you get any solution on this. I required this feature too.

jsacksick’s picture

Status: Active » Fixed

Returning NULL is possible...

   * @return \Drupal\commerce_product\Entity\ProductVariationInterface|null
   *   The default variation, or NULL if none found.

The add to cart form has then the following code:

    $default_variation = $product->getDefaultVariation();
    if (!$default_variation) {
      return [];
    }

The add to cart form isn't built if there's no default variation.

Status: Fixed » Closed (fixed)

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