2

I have an observer attached to sales_order_place_before and from that event I am getting the order items using

   $event = $observer->getEvent();
   $order = $event->getOrder();
   $items = $order->getQuote()->getAllItems();

When I log these items in a foreach like so I get double the amount of products. That is, I get the configurable AND the simple product

foreach($items as $item){
            $product = $item;
            $sku = $product->getSku();
            $name = $product->getName();
}

If I change the $items call to be $items = $order->getQuote()->getAllVisibleItems(); then I can get just the configurable products, but I want to get just the simple products.

My question is, how can I get just the simple products from the order.

2 Answers 2

2

I think there is no direct method to exclude the configurables, since the logic in Magento is that the configurable is the "main" product where the price is at. The associated simple product might have a wrong price configured. In any case, if you want to still get these items and not the configurables, your best bet is to use the getAllItems() method and then afterwards kick out the configurables:

 $event = $observer->getEvent();
 $order = $event->getOrder();
 $items = $order->getQuote()->getAllItems();
 $itemsExcludingConfigurables = array();
 foreach ($items as $item) {
    if ($item->getTypeId() != 'configurable') {
       $itemsExcludingConfigurables[] = $item;
    }
 }
2
  • I don't think this is entirely accurate regarding the configurable product having the 'price'. If you have a configurable with a price, say $80. and a simple with a price of $50. When you select that simple products attribute from the product page the price will change to the simple price of $50. That said the getTypeId method worked. Thanks.
    – James
    Oct 30, 2013 at 1:52
  • @James You are probably using an extension then to change the price to the simple product price, since normally, the price of a configured product in Magento is the configurable price +- the price difference configured for that select option which is configured on the configurable product page in the backend. Glad that it works, though :)
    – mpaepper
    Oct 30, 2013 at 9:37
10

it is not that easy, because one quote item can have more than one children (grouped, bundle items).

Therefore I suggest something like:

$returnItems = array();
foreach($order->getQuote()->getAllVisibleItems() as $item){
        if($item->getHasChildren()) {
            foreach($item->getChildrenItems() as $child) {
                $returnItems[] = $child;
            }
        } else {
            $returnItems[] = $item;
        }
}
2
  • This is a great solution, but I think $item->getChildren() should be $item->getChildrenItems() (at least this was the case in EE1.14)
    – Adelmar
    Nov 8, 2014 at 3:53
  • 3
    Just edit it :-) These small mistakes are part of the "something like this" :-) Thanks for pointing out! Nov 8, 2014 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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