1

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>";}
1

Please go to this link it may help you

https://www.atwix.com/magento/accessing-custom-attribute-at-checkout-or-cart/

  • Thanks. I had seen this but it emphasises my question. Again, this link does things through a module while my approach didn't bother with a module. Why should I use a module? – PedroKTFC May 10 '14 at 12:38
  • You want a custom code , so you can find appropriate code from this module dude – Ami Kamboj May 10 '14 at 14:00
  • It would be great if you can write down the most important parts of the link into your answer. If the links vanishes in the future, your answer is still valuable. – Fabian Blechschmidt May 30 '14 at 10:39
1

Update:

What magento does is to load all the quote items on the cart pages and load all attributes, which are defined in app/code/core/Mage/Sales/etc/config.xml at the XPath: /config/global/sales/quote/item/product_attributes.

What you then do is:

Mage::getModel('catalog/product')->load($this->getProduct()->getId())->getAttributeText('color');

What this does is to load the product again. This is a performance killer. What you want instead is, to add your attributes (color and size) to this XPath. And for THIS, you need an config.xml.

Old:

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

Yes.

You want more infos? To load attributes for quote_items you have to add some nodes into the config-XML. To do this you have either to edit an existing config.xml (NOOOOOOOOO!!!) or have your own one.

  • Thanks Fabian but I still haven't understood why. My little bit of code above does what I want without a module. Why then should I use a module, what extra does it give me? – PedroKTFC May 10 '14 at 17:16
  • updated ... and a few more chars to have enough to ping you. – Fabian Blechschmidt May 10 '14 at 22:03
  • Fabian, just a nudge. I posted a new answer explaining how far I've got and what I now don't understand. I'd much appreciate your thoughts if you can spare the time. – PedroKTFC May 15 '14 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.