6

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.

  • sorry unable to comment and hence putting my thought in the answer. Have you tried print_r($item) to see if it has ean in it? and if yes, what is the value? – dee Jun 24 '13 at 15:05
  • Hi, yes I've tried it but I got such a big array it wouldn't load in my browser. I have managed to get it to work with my answer below. – user1669256 Jun 24 '13 at 15:13
  • I don't have enough reputation to make an answer but the code I used is below. I will update the answer properly when I the site lets me. – user1669256 Jun 24 '13 at 15:15
  • $cartItems = Mage::getSingleton('checkout/session')->getQuote()-getAllItems(); foreach ($cartItems as $item) { $ean = Mage::getModel('catalog/product')->load($item->getProduct()->getId())->getEan(); echo $ean; } – user1669256 Jun 24 '13 at 15:16
12

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.

  • 2
    You want to add the attribute to the attribute list which is loaded and then remove the damn load() inside the loop! atwix.com/magento/… see the config.xml snippet! – Fabian Blechschmidt Aug 28 '14 at 10:25
7

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");
4

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); 
2

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/

  • Hi, thank you for your answer. I have tried both getEan() and getData('ean') but I am still getting an empty output. Is there anything I've missed out? I'm using this code in OnepageController.php in the function: public function saveOrderAction() – user1669256 Jun 24 '13 at 14:29
  • var_dump the output or use join to echo. Or better yet - Mage::log the output and paste here. – philwinkle Jun 24 '13 at 14:34
  • If I use $itemEan = $item->getSku(); var_dump($itemEan); I get the output: string(9) "123456789" which is the sku for the product I'm testing. If I use $itemEan = $item->getData('ean'); var_dump($itemEan); what gets displayed is 'NULL'. – user1669256 Jun 24 '13 at 14:56
1

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!

  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. – Amasty Aug 28 '14 at 10:56
  • I believe it's something to do with your function selection. From my understanding, if you want to show a [text] attribute, you should use getData. If you want to get a DropDown value, you should be using getAttributeText. This is how it works for me, regardless of the view. I have successfully used these two functions on PRODUCT view and checkout Page as well as the cart page. So, my answer still answers your question and I don't get why the down vote! – Mohamed Elgharabawy Aug 29 '14 at 8:29

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.