nano
nano

Reputation: 2521

Magento - Get product info from cart item

I've an observer where I want to check some info from my cart items.

I want to load the product attributes for these items:

$items = $observer->getCart()->getItems();
foreach ($items as $item) {
    $product = $item->getProduct();
    if ($product->getData('my_attribute')) { 
        // My logic
    }
}

But my business logic is never executed as my_attribute is not loaded in the $item->getProduct() information.

I've tried to add in the config.xml file this code:

<config>
    <global>
        <sales>
            <quote>
                <item>
                    <product_attributes>
                        <my_attribute />
                    </product_attributes>
                </item>
            </quote>
        </sales>

The only code that works for me is loading the product individually:

$product = Mage::getModel('catalog/product')->load($item->getProductId());

What's the difference between loading the product from catalog\product and the product contained in my cart items?

Upvotes: 4

Views: 7279

Answers (1)

Sharfaraz Bheda
Sharfaraz Bheda

Reputation: 457

You can follow this instruction:

enter image description here

This can be done with XML by adding the following code to your config.xml:

<global>
    <sales>
        <quote>
            <item>
                <product_attributes>
                    <attribute1 />
                    <attribute2 />
                </product_attributes>
            </item>
        </quote>
    </sales>
</global>

where attribute1 and attribute2 are your attribute codes. Then you can access attribute using the code below:

$item->getData('attribute1');
//if you use observer or quote object:
$item->getProduct()->getData('attribute1');

Upvotes: 1

Related Questions