Magento - 从$item获取产品选项


Magento - Get Product options from $item

在购物车页面上,有以下foreach循环:

foreach($this->getItems() as $_item) {
}
我需要

获取这些项目的产品选项,我已经尝试了几种方法,但我无法检索我需要的结果。

我试过:

foreach($this->getItems() as $_item) {
    print_r($_item->getProductOptions());
}

和:

foreach($this->getItems() as $_item) {
    print_r($_item->getOptionList());
}

还有其他我可以使用的功能吗?

尝试使用:

$_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());

这可能会让你开始正确的方向...

$productSku = "ABCDE";
$product = Mage::getModel('catalog/product');
$productId = $product->getIdBySku( $productSku );
$product->load($productId);
/**
 * In Magento Models or database schema level, the product's Custom Options are
 * executed & maintained as only "options". So, when checking whether any product has
 * Custom Options or not, we should check by using this method "hasOptions()" only.
 */
if($product->hasOptions()) {
    echo '<pre>';
    foreach ($product->getOptions() as $o) {
        $optionType = $o->getType();
        echo 'Type = '.$optionType;
        if ($optionType == 'drop_down') {
            $values = $o->getValues();
            foreach ($values as $k => $v) {
                print_r($v);
            }
        }
        else {
            print_r($o);
        }
    }
    echo '</pre>';
}

也许是这样的:

foreach($items as $product) {
    $options = $product->getProduct()->getTypeInstance(true)->getOrderOptions($product->getProduct());
    if ($options)
    {
        if (isset($options['options']))
        {
            $result = $options['options'];
        }
        if(count($result)>0){
            foreach($result as $key =>$value){
                $resultoption =  $value['value'];
        }
    }
}

目前的答案对我来说是不行的。无论$_item是什么,都可能没有getProduct()方法。

另一方面,您可能有一个可以直接加载的可用id。在我的示例中,我需要从 $_items = $this->helper('catalog/product_compare')->getItemCollection() 中的项目获取产品对象。

这使我能够使用: <?php $product = Mage::getModel('catalog/product')->load($_item->getId()) ?>

您无法在 cart.phtml 上获取选项列表,您必须更新/编辑以下文件以获取选项列表:

app'design'frontend'YOUR_PACKAGE_NAME'YOUR_TEMPLATE_NAME'template'checkout'cart'item'default.phtml

希望它能有所帮助!