php Magento - 从 $item 获取产品选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19494473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:34:45  来源:igfitidea点击:

Magento - Get Product options from $item

phpmagento

提问by Karl

On the cart page there's the following foreach loop:

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

foreach($this->getItems() as $_item) {

}

I need to get the product options for these items, I've tried a few methods but I'm unable to retrieve the results I need.

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

I've tried:

我试过了:

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

And:

和:

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

Are there any other functions I could use?

我可以使用其他任何功能吗?

回答by David

Try using:

尝试使用:

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

回答by Matthew

This might get you started in the right direction...

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

$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>';
}

回答by Christoffer Bubach

Maybe like this:

也许是这样的:

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'];
        }
    }
}

回答by Jonathan

The current answer as it stands is a no-go for me. Whatever $_itemis may not have the getProduct()method.

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

On the other hand, you will likely have an idavailable that you can load directly from. In my example, I needed to get the product object from an item in $_items = $this->helper('catalog/product_compare')->getItemCollection().

另一方面,您可能会有一个id可以直接加载的可用资源。在我的示例中,我需要从$_items = $this->helper('catalog/product_compare')->getItemCollection().

This enables me to use: <?php $product = Mage::getModel('catalog/product')->load($_item->getId()) ?>

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

回答by Rajiv Ranjan

You can not get option list on cart.phtml, you have to update/edit below file for option list:

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

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

Hope it will help!

希望它会有所帮助!