Home » Php » php – Magento – Get Product options from $item

php – Magento – Get Product options from $item

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&Answers:

Try using:

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

Answer:

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

Answer:

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

Answer:

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()) ?>

Answer:

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!