0

I'm trying to get parent_item_id but it is returning null for all simple child products.

    $order = $observer->getEvent()->getOrder();
    $items = $order->getAllVisibleItems();
    foreach ($items as $item) {
            $itemsData[] = [
                'name' => $item->getName(),
                'sku' => $item->getSku(),
                'quote_item_id' => $item->getQuoteItemId(),
                'parent_item_id' =>$item->getParentItemId(),
                'qty_ordered' => $item->getQtyOrdered(),
                'product_type' => $item->getProductType(),
                'weight' => $item->getWeight(),
                'price' => $item->getPrice()
            ];        
    }

I'm getting all item attributes in my $itemsData array except parent_item_id, though I've 4 children of a Bundle product and 1 child of a configurable product in Cart.

I'm doing it in sales_order_place_before observer. I tried getAllItems() and getItems() as well but no luck. Also confirmed from database, parent_item_id exists there for quote_item_id, but just not able to retrieve via getter method.

1 Answer 1

0

I placed test order for two configurable items. When I use $order->getAllVisibleItems() I get the two parent rows returned, so I think parent_item_id will always be null.

enter image description here

If I use $order->getItems() I get four rows returned, each parent and each child.

enter image description here

But the parent_item_id is the parent row's item_id. I added $item->getItemId() to show the next row is linked to the row above. It seems sku for the child is saved as sku for the parent, so I'm not sure what your end goal is, but maybe you don't really need parent_item_id anyway. e.g. if you use this data later and need to load product repository, you should just use get($sku) method instead of getById($productId).

enter image description here

My test code for the images:

$orderRepositoryInterface = $objectManager->get(\Magento\Sales\Api\OrderRepositoryInterface::class);
    $order = $orderRepositoryInterface->get(154);
    $items = $order->getAllVisibleItems();
    
    foreach ($items as $item) {
                echo 'name: '. $item->getName() .'<br />';
                echo 'sku: ' . $item->getSku() .'<br />';
                echo 'order item_id: ' . $item->getItemId() . '<br />';
                echo 'quote_item_id: ' . $item->getQuoteItemId() .'<br />';
                echo 'parent_item_id: ' . $item->getParentItemId() .'<br />';
                echo 'product_id: ' . $item->getProductId() . '<br />';
                echo 'qty_ordered: ' . $item->getQtyOrdered() .'<br />';
                echo 'product_type: ' . $item->getProductType() .'<br />';
                echo 'weight: ' . $item->getWeight() .'<br />';
                echo 'price: ' . $item->getPrice() .'<br />';
    }
4
  • for me both $order->getAllVisibleItems() and $order->getItems() are showing the same number of items, all four if we consider your example. Can you please show your code? Also which Magento version you're using? Apr 4, 2021 at 12:06
  • My end goal is to get the same output as you've mentioned in your first example of $order->getAllVisibleItems(). But for me, I'm getting all four items, both parent and child. Apr 4, 2021 at 12:11
  • Maybe because it is sales_order_place_before? There is also sales_order_place_after you could try. In those I just used your code but not in observer (just changed output to echo instead of an array), I loaded the order by repository. So, maybe you should try: $orderId = $observer->getEvent()->getOrder()->getEntityId(); and then load order repository by $orderId
    – bernieu2
    Apr 4, 2021 at 13:23
  • I tried sales_order_place_after as well but no luck. I think the issue is with $order object only, even $observer->getEvent()->getOrder()->getEntityId(); is also returning nothing in sales_order_save_before/after. Solution: From $order, I get the $quote as $quote = $this->_quoteRepository->get($order->getQuoteId()); and the I'm getting correct data in getAllVisivleItems(). So I'm using it now. Apr 5, 2021 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.