Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

add this to your module's config.xml:

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

You will be able to access the attribute through:

$_item->getProduct()->getData("custom_attribute");

If you are in product page, then for drop-down attribute simply add

echo $_product->getAttributeText('attribute_code');

for text attribute add

echo  $_product->getData('attribute_code');

If you are not in product page then you have to load product by product id first.

$_product = Mage::getModel('catalog/product')->load($id); 

getAttributeText is for dropdown fields. For text fields use magic getters:

$item->getEan();

or

$item->getData('ean');

For your multi-select the getData should return an array - which you then have to foreach over, or you could implode it. If you're in a context where you don't know what type is being returned (though I'm not sure how this would even be possible) you can test it via the following:

Get attribute object – if you only have attribute code

$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', $_attribute_code);

Get Multi-Select Values

if($attribute->getFrontendInput() == 'multiselect') {
  $multiSelectArray = $_product->getAttributeText($_attribute);
  #var_dump($multiSelectArray);
  echo implode(', ',$multiSelectArray);
}

Source: http://www.magentodeveloperleedsuk.co.uk/get-drop-down-list-and-multi-select-values-for-magento-product-attributes/

On the product view as well as the checkout page and the cart page on Magento Community Edition 1.7.0.2. I managed to get my custom attribute to appear by doing this:

$custom_attribute = $_product->getData('custom_attribute');

Hope this would be of help!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top