Magento – Magento checkout – getting custom attribute value

checkoutcontrollersmagento-1.7onepage-checkout

I am working in the OnepageController.php

What I needed was in the checkout process was to get values of some customer created attributes for the products in the current order. I have been able to get the system attribute 'sku' to show but I can't get custom attribute values to show.

My code so far is:

    $helper = Mage::helper('checkout/cart');
    $items = $helper->getCart()->getItems();

    foreach ($items as $item) {
        $itemSku = $item->getSku();

        echo $itemSku."<br/>";
  }

I have ran this in FireFox using Firebug and it does display the SKU value. What I need however is the value of a custom attribute created in the backend. I have tried to replace:

$itemSku = $item->getSku();

to:

$itemEan = $item->getAttributeText('ean');

Where 'ean' is the attribute identifier. This attribute is a text field. When the checkout is ran again there are no errors but noting displayed (apart from the break line html tag).

I also have another custom attribute which is a 'multi-select' attribute. Does how we get the attribute value for this change?

Any help displaying this is much appreciated.


UPDATE

I can't write an answer because of no reputation but I have found a solution which works for me if it helps anyone. The code below worked for me:

    $cartItems = Mage::getSingleton('checkout/session')->getQuote()-getAllItems();
    foreach ($cartItems as $item) {
        $ean = Mage::getModel('catalog/product')->load($item->getProduct()->getId())->getEan();
        echo $ean;
    }

where 'getEan()' is the name of my custom attribute starting with a capital letter.

Thanks to all for their input.

Best Answer

After some research I managed to find some code which helped, which is below:

    $cartItems = Mage::getSingleton('checkout/session')
          ->getQuote()
          ->getAllItems();
    foreach ($cartItems as $item) {
        $ean = Mage::getModel('catalog/product')->load($item->getProduct()->getId())->getEan();
        echo $ean;
    }

Where getEan is the name of my attribute with the first character as a capital letter.

Thank you all for the suggestions.

Related Topic