问题
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