Score:0

Update another line items quantity when line item updated

ke flag

Drupal 9, Drupal Commerce 2.26 I have an event subscriber, listening to CartEvents .. specifically CartEvents::CART_ORDER_ITEM_UPDATE

I have products of one type that reference products of another type.

The goal is when user updates order item for product of type 'class' in the cart, the order item for matching material_kit quantity is updated to match.

I have similar event subscribers, that allows me to create a matching order item when a product is added to cart, or when a order item is removed, the matched order item is also deleted. Only update of quantity to match updates to another is not working.

The event fires as expected. The execution flow gets through the conditionals, update the quantity of the 2nd line item, but the updated quantity is never reflected. Its like it never happened.

I could possibly remove and recreate a new order item for the product, but would prefer not to, and want to understand what is happening.

So how to update another order item's quantity, when a order item quantity is updated in the cart?

/**
   * Manage referenced material kit quantity on class product quantity change
   *
   * @param \Drupal\commerce_cart\Event\CartOrderItemUpdateEvent $event
   *   The cart event.
   */
  public function onCartOrderItemUpdate(CartOrderItemUpdateEvent $event) {
    $order_item = $event->getOrderItem();
    $product_variation = $order_item->getPurchasedEntity();
    $productType = $product_variation->get('type')[0]->get('target_id')->getValue();
    if ($productType == 'class') {
      $class_quantity = $order_item->getQuantity();
      $cart = $event->getCart();
      $product = $product_variation->getProduct();
      $material_kit_field_value = $product->get('field_material_fees')->getValue();
      if (!empty($material_kit_field_value[0]['target_id']) && empty($payable_to_instructor[0]['value'])) {
        $material_kit_product = Product::load($material_kit_field_value[0]['target_id']);
        if (!empty($material_kit_product)) {
          $material_kit_variations = $material_kit_product->getVariations();
          $material_kit_variation_id = $material_kit_variations[0]->id();
          foreach ($cart->getItems() as $key => $oi) {
            $pv = $oi->getPurchasedEntity();
            $pt = $pv->get('type')[0]->get('target_id')->getValue();
            if ($pt == 'material_kit') {
              if ($pv->id() == $material_kit_variation_id) {
                // execution flow gets here, but the updated quantity doesn't "stick"
                $oi->setQuantity($class_quantity);
                $this->cartManager->updateOrderItem($cart, $oi);
                // why isn't the order item update working? ^^
              }
            }
          }
        }
      }
    }
  }
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.