5 Comments

Magento checkout – getting custom attribute value

display attribute to specific customer group
Magento order confirmation emails are not working

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.

Related Posts

  • Contact form redirects to different page after submitting the form (1)
    I have contact us page which includes map, form, and some text. the page url is /contacts_page, but when i submit the form it goes to some different url i.e /contacts/index and this new redirect page shows thank you message and contact form. I want to make the url to redirect on same […]
  • Add html text formatting to titles (1)
    I'm trying to add text formatting to a Title on my checkout page. For example the shipping method Title is: STANDARD SHIPPING RATE (free over $100) I would like it to display with the '(free over $100)' to appear as smaller font size. The checkout is a custom extension but the […]
  • Firefox adding trailing slash and the URL breaks (2)
    I got a custom menu plugin in Magento2 as below. class TopmenuPlugin { public function afterGetHtml(MagentoThemeBlockHtmlTopmenu $topmenu, $html) { $html = ''; $html .= "<li class="level0 nav-1 level-top parent ui-menu-item">"; $html .= "<a […]
  • showing text on page (0)
    I am using this code to show a image of a product attribute "brand image" <?php $brand = $_product->getAttributeText('brand'); $brandImage = str_replace(' ', '_', $brand) . ".jpg"; $brandLink = strtolower(str_replace(' ', '-', $brand)) . ".html"; $brandBaseUrl = […]
  • showing text on page (0)
    I am using this code to show a image of a product attribute "brand image" <?php $brand = $_product->getAttributeText('brand'); $brandImage = str_replace(' ', '_', $brand) . ".jpg"; $brandLink = strtolower(str_replace(' ', '-', $brand)) . ".html"; $brandBaseUrl = […]
  • remove multiple price value from product page (1)
    in this configurable product under check delivery Availabilty text, you can see price Rs 100, 2 times. i want to remove one and i want to display only one price as like in this link view.phtml : <?php ?> <?php $_helper = $this->helper('catalog/output'); […]
  • How to hide the tab when no value is entered in the text field? (1)
    I have added a text-field attribute in product-collateral tab in details page with following code: <?php $_helper = $this->helper('catalog/output'); ?> <?php $_product = $this->getProduct(); ?> <script type="text/javascript"> var optionsPrice = new […]
  • remove multiple price value from product page (1)
    in this configurable product under check delivery Availabilty text, you can see price Rs 100, 2 times. i want to remove one and i want to display only one price as like in this link view.phtml : <?php ?> <?php $_helper = $this->helper('catalog/output'); […]
  • How can I add form on my custom Shipping mode in Magento2? (0)
    I'm currently trying to figure out how I can add a form (html with like input text etc) for each of my shipping carrier. I already add custom shipping carrier, with the right way I think (config / system / model with collectRates()) but for each carrier, I must have a different […]
  • How am I suppose to access product purchase variables? (1)
    I need to send an mail to customer on purchase.I am customizing the Invoice template.I need to show the product that he/she purchased when the email is sent. Whenever a customer purchases a product from my store, system send sends an invoice mail to customer.However I need to […]
display attribute to specific customer group
Magento order confirmation emails are not working

5 Comments on this article

  • User November 12, 2016

    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.

    Reply

  • User November 12, 2016

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

    Reply

  • User November 12, 2016

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

    Reply

  • User November 12, 2016

    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/

    Reply

  • User November 12, 2016

    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!

    Reply

Add a comment