Add to cart button altering doesn't work with AJAX product variations switching

Created on 23 August 2024, 8 months ago
Updated 12 September 2024, 8 months ago

Problem/Motivation

the functionality only works for the very first variation of a product. The add to cart form then has its "orderable" status altering, not the correct status of the individual product variations

Steps to reproduce

Go to a product that has several product variations, some of the orderable, others not
Switch the product variations and see the "Add to cart" button stays as it is, eventhough altering is enabled

Proposed resolution

Presumably this needs to be altered in a different hook. Let's see!

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code (commerce_product_availability)

Created by

🇩🇪Germany Anybody Porta Westfalica

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

Merge Requests

Comments & Activities

  • Issue created by @Anybody
  • 🇩🇪Germany Anybody Porta Westfalica

    Ok we don't need another hook, this seems to be the right one, but

    $variation = $orderItem->getPurchasedEntity();
    

    always returns the first variation of the current product!

  • 🇩🇪Germany Anybody Porta Westfalica

    Okay I think we need to do it differently:

    From AddToCartForm:

      public function buildForm(array $form, FormStateInterface $form_state) {
        $original_form = $form;
        $form = parent::buildForm($form, $form_state);
        /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
        $order_item = $this->entity;
        // The widgets are allowed to signal that the form should be hidden
        // (because there's no purchasable entity to select, for example).
        if ($form_state->get('hide_form')) {
          $form['#access'] = FALSE;
        }
        else {
          $selected_variation = $form_state->get('selected_variation');
          // If the order item references a different variation than the one
          // currently selected, and the selected variation is supposed to use
          // a different order item type, rebuild the form.
          if ($selected_variation && $order_item->getPurchasedEntityId() != $selected_variation) {
            /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $selected_variation */
            $selected_variation = $this->entityTypeManager->getStorage('commerce_product_variation')->load($selected_variation);
            if ($selected_variation->getOrderItemTypeId() !== $order_item->bundle()) {
              /** @var \Drupal\commerce_order\OrderItemStorageInterface $order_item_storage */
              $order_item_storage = $this->entityTypeManager->getStorage('commerce_order_item');
              $order_item = $order_item_storage->createFromPurchasableEntity($selected_variation);
              $this->prepareOrderItem($order_item);
              $this->setEntity($order_item);
              $form_display = EntityFormDisplay::collectRenderDisplay($order_item, $this->operation);
              $this->setFormDisplay($form_display, $form_state);
              $form = $original_form;
              $form = parent::buildForm($form, $form_state);
            }
          }
        }
    
        return $form;
      }
    
  • 🇩🇪Germany Anybody Porta Westfalica

    Found really really helpful documentation here btw: 🐛 Document how to alter forms when hook_form_alter won't work Needs work

  • Status changed to Needs review 8 months ago
  • 🇩🇪Germany Anybody Porta Westfalica

    Found the solution in \Drupal\commerce_cart\Form\AddToCartForm::buildForm()

  • 🇩🇪Germany Anybody Porta Westfalica

    Works as expected now!

  • Pipeline finished with Skipped
    8 months ago
    #262976
  • 🇩🇪Germany Anybody Porta Westfalica
  • Status changed to Fixed 8 months ago
    • anybody committed a2d2bf1d on 1.x
      Issue #3469910 by anybody: Add to cart button altering doesn't work with...
  • Pipeline finished with Skipped
    8 months ago
    #268474
    • anybody committed f1328e29 on 1.x
      Issue #3469910 by anybody: Add to cart button altering doesn't work with...
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024