Magento – Do I need a module to display product attributes in the shopping cart

cartmagento-1shopping-cart

I have a store with products that have attributes for colour and size that users have to select. I want to display their selection in the shopping cart and, googling around, all the suggestions for how to do this suggest using a module (for example, see here http://www.magentocommerce.com/boards/viewthread/43580/).

However, I seem to have managed it without using a module. I've simply added the following code to app/design/frontend/myarea/default/template/checkout/cart/item/default.phtml. Note I have other products, such as gift cards, that don't have these attributes.

    $_colour = Mage::getModel('catalog/product')->load($this->getProduct()->getId())->getAttributeText('color');
    $_size = Mage::getModel('catalog/product')->load($this->getProduct()->getId())->getAttributeText('sizes');
    if ($_colour) {echo "<div>" . $this->__('Colour: ') . $_colour . "</div>";}
    if ($_size) {echo "<div>" . $this->__('Size: ') . $_size . "</div>";}

Is there something I'm missing in these other posts? I'm concerned my solution may have a flaw that will show up at some point.


Edit:

Thanks guys but now I'm really baffled!

I've put the following in my config.xml file:

<global>
    <sales>
    <!-- add product attributes to cart layout -->
        <quote>
            <item>
                <product_attributes>
                    <color />
                    <sizes />
                </product_attributes>
            </item>
        </quote>
    </sales>
</global>

If I have the following in my default.phtml file, I only get the size. The colour isn't displayed. (If I remove the sizes from my config.xml, the size isn't displayed as I would expect.)

    $_colour = $_item->getProduct()->getAttributeText('color');
    $_size = $_item->getProduct()->getAttributeText('sizes');
    if ($_colour) {echo "<div><strong>" . $this->__('Colour: ') . "</strong>" . $_colour . "</div>";}
    if ($_size) {echo "<div><strong>" . $this->__('Size: ') . "</strong>" . $_size . "</div>";}

Now the bizarre thing is if I replace $_item by $this, it works! But it ignores whatever's in my config.xml file.

    $_colour = $this->getProduct()->getAttributeText('color');
    $_size = $this->getProduct()->getAttributeText('sizes');
    if ($_colour) {echo "<div><strong>" . $this->__('Colour: ') . "</strong>" . $_colour . "</div>";}
    if ($_size) {echo "<div><strong>" . $this->__('Size: ') . "</strong>" . $_size . "</div>";}

Best Answer

Related Topic