简体   繁体   中英

Magento - Get Product options from $item

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

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?

尝试使用:

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

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

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

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

On the other hand, you will likely have an id available 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() .

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

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

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

Hope it will help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM