selected_variation also need to be set when Product has only one variation

Created on 27 May 2022, about 2 years ago
Updated 18 June 2024, 10 days ago

selected_variation also need to be set when Product has only one variation

The BUG appears here:
\Drupal\commerce_product\Plugin\Field\FieldWidget\ProductVariationAttributesWidget::formElement

when Product has only one variation, also need:
$form_state->set('selected_variation', $selected_variation->id());
Otherwise, the following method will have problems:
\Drupal\commerce_product\Plugin\Field\FieldWidget\ProductVariationWidgetBase::ajaxRefresh

This is necessary when selling custom product with a refresh price

๐Ÿ› Bug report
Status

Needs review

Version

2.30

Component

Product

Created by

๐Ÿ‡จ๐Ÿ‡ณChina yunke

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States Dave Kopecek

    Dave Kopecek โ†’ made their first commit to this issueโ€™s fork.

  • ๐Ÿ‡ฎ๐Ÿ‡ฑIsrael jsacksick

    This is necessary when selling custom product with a refresh price

    Can you elaborate? "selected_variation" isn't set in this case because that is used by the ajax refresh callback, but there are no attributes elements in case of a single variation?

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States Dave Kopecek

    >> Can you elaborate? "selected_variation" isn't set in this case

    In my use case we have a custom my_module_form_commerce_order_item_add_to_cart_form(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id that has:

        $vid = $form_state->get('selected_variation');
        $variation = \Drupal\commerce_product\Entity\ProductVariation::load($vid);
    

    This broke when a new product with only a single variation was created. Is there a better way to get the current variation from the passed parameters?

  • ๐Ÿ‡ฎ๐Ÿ‡ฑIsrael jsacksick

    I guess we can consider your fix but in the meantime you could just have a condition that checks if $form_state->has('selected_variation'); otherwise you can fallback to the first variation referenced from the product.

  • Status changed to Needs review 10 days ago
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 10 days ago
    795 pass
  • ๐Ÿ‡ฎ๐Ÿ‡ฑIsrael jsacksick

    Does the attached patch help?

Production build 0.69.0 2024