- 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
- Merge request !15Add fix from \Drupal\commerce_cart\Form\AddToCartForm::buildForm() → (Merged) created by Anybody
- Status changed to Needs review
8 months ago 5:37pm 23 August 2024 - 🇩🇪Germany Anybody Porta Westfalica
Found the solution in
\Drupal\commerce_cart\Form\AddToCartForm::buildForm()
- Status changed to Fixed
8 months ago 5:39pm 23 August 2024 - Merge request !19Update commerce_product_availability_webform_request.module → (Merged) created by Anybody
Automatically closed - issue fixed for 2 weeks with no activity.