Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - Get Product options from $item

Tags:

php

magento

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?

like image 740
Karl Avatar asked Oct 21 '13 12:10

Karl


People also ask

How do I get selected custom options in Magento 2?

The custom option is a native feature of Magento and you can add Custom option from Admin side in product Add/edit section.

How can I get option ID in Magento 2?

In order to get attribute option id by the label, it is required to use \Magento\Catalog\Model\Product Factory class in the construct. A huge variety of attribute types are available such as dropdown, visual swatch, multi-select, etc wherein you can add multiple options in a single attribute.

What is product option Magento?

When you add custom options to a product in Magento 2, you give your customers the opportunity to choose product options as to their needs without relying on the product attributes. Customizable options allow you to categorize products, create cart price rules and dynamic categories rules.


2 Answers

Try using:

$_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());
like image 95
David Avatar answered Oct 15 '22 21:10

David


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>';
}
like image 36
Matthew Avatar answered Oct 15 '22 20:10

Matthew