4

I see only product titles of products that are selected in product_options property of order item. Is there any way to get ids or skus of those products?

0

Instead of using $order->getAllItems() use the sales item collection itself:

$collection = Mage::getResourceModel('sales/order_item_collection')->addFieldToFilter('order_id',array('eq'=>$order_id));

Where $order_id is the entity_id of the sales_order.

To use this collection, iterate over it - all items are available, even children items of bundles and configurables -- e.g. not just visible items:

foreach($collection as $_item){
        var_dump($_item->getSku());
}
  • Hmm, confusing answer... getAllItems() will return all the items from the sales collection even children items of bundles and configurables. It is the getAllVisibleItems() method that returns only parent items. – BlueC Apr 17 '17 at 12:51
  • 1
    @BlueC 4 year old answer but thanks for the feedback – philwinkle Apr 17 '17 at 13:41
6

Actually, there is a way to get the option skus from the order item - and without having to load any products. Observe!

foreach($order->getAllItems() as $item) {
    $options = $item->getProductOptions();
    $optionIds = array_keys($options['info_buyRequest']['bundle_option']);
    $types = Mage_Catalog_Model_Product_Type::getTypes();
    $typemodel = Mage::getSingleton($types[Mage_Catalog_Model_Product_Type::TYPE_BUNDLE]['model']);
    $typemodel->setConfig($types[Mage_Catalog_Model_Product_Type::TYPE_BUNDLE]);
    $selections = $typemodel->getSelectionsCollection($optionIds, $item);
    $selection_map = array();
    foreach($selections->getData() as $selection) {
        if(!isset($selection_map[$selection['option_id']])) {
            $selection_map[$selection['option_id']] = array();
        }
        $selection_map[$selection['option_id']][$selection['selection_id']] = $selection;
    }
    $chosen_skus = array();
    foreach($options['info_buyRequest']['bundle_option'] as $op => $sel) {
        $chosen_skus[] = $selection_map[$op][$sel]['sku'];
    }
    echo "Item ".Mage::getResourceModel('catalog/product')->
        getAttributeRawValue($item->getProductId(), 'sku', $item->getStoreId()).
        " has chosen SKUs: ".implode(', ', $chosen_skus)."\n";
}
  • Have not tried it but it might work. Thanks for the effort :) – LDusan Nov 14 '13 at 22:18
2

The items are stored in the sales_flat_order_item table as seperate rows (items), the items have the parent_item_id field set to the ID of the bundle product row.

  • Don't access the database directly. – philwinkle Jun 26 '13 at 17:24
  • It explains some things but is not a direct answer. Thanks anyway. – LDusan Jun 26 '13 at 23:00

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.