How to get SKU of custom options selected for item order programatically in Magento

 ̄綄美尐妖づ 提交于 2019-12-07 11:11:03

问题


I have a product with multiple custom option customers can choose. For examples:

Product: Flower Basket of 10 flowers (SKU 100) Custom options:

  • Red roses qty: Drop down with 0 to 10 (each with SKU 200)

  • Purple Roses qty: Drop down with 0 to 10 (each with SKU 300)

  • Pink Tulips qty: Drop down with 0 to 10 (each with SKU 400)

This way customer can build their own basket. However, now I have to export my orders for warehouse system and I need to list Custom Option's SKU with value(qty). While I can get the label and value from Order Item. There is no SKU.

I am getting my items like so:

$orders = Mage::getModel('sales/order')->getCollection()
              ->addAttributeToFilter('status', array('eq' => 'processing'));
foreach ($orders as $order) {

  foreach ($order->getAllItems() as $order_item) {

      $optionsArr = $order_item->getProductOptions();

       if (count($optionsArr['options']) > 0) {
            foreach ($optionsArr['options'] as $option) {
                $optionTitle = $option['label'];
                $optionId = $option['option_id'];
                $optionValue = $option['value'];
                // no SKU ?!?! 
            }
        }

  }

}

Any ideas how to get the SKU for every Custom Option selected?


回答1:


Try loading the product and getting an option collection.

foreach($order->getAllItems() as $item) {
    $product = Mage::getModel('catalog/product')->load($item->getProductId());
    $options = $product->getProductOptionsCollection();
    foreach($options as $option) {
        echo $option->getSku();
    }
}



回答2:


Can you please try this way.

foreach($order->getAllItems() as $_item) {
$customOptions = $_item->getProductOptions();

        foreach ($customOptions['options'] as $_eachOption) {
            $objModel = Mage::getModel('catalog/product_option_value')->load($_eachOption['option_value']);
            print_r($objModel->getData());
        }
}


来源:https://stackoverflow.com/questions/14225368/how-to-get-sku-of-custom-options-selected-for-item-order-programatically-in-mage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!