• Eyelash / php jquery image management
  • Magento custom attributes display on product page

    The flexibility created by the attribute feature in Magento allows limitless possibilities : vairous product types can be created and each of them can go with specific attributes, meaning specific form fields for the admin, that are inserted in the product view (template/product/view.phtml) for public display. The functions availble for each user defined attributes are very simple to guess : they are dynamically created for each attribute create using , as explained in the Pratthost developer zone blog

    <?php echo $_product->getAttributeName() ?>
    There is one thing different here though. If your Attribute Code is “shirt_size”, then you would use getShirtSize(). Remove the underscores and capitalize the first letter of each word. It is picky that way. if you use getshirtsize(), it won’t work.

    If you are using a dropdown or a multiple select, you have to call it a little differently:

    
    
    <?php echo $_product->getAttributeText(‘shirt_size’) ?>

    This method requires the actual Attribute Code. If you are displaying the value from a dropdown, you’ll get exactly what you need with this call. If you are wanting to display the values from a multiple select, it will return an array.

    Inner workings

    How does this work ? well the attribute function is a method of the Mage_Catalog_Model_Product, which isn extension of Mage_Catalog_Model_Abstract, itself extending Mage_Core_Model_Abstract, itself extending Varien_Object where we find the famous __call function that traps all undeclared or undefined  functions from the class : this is where the logic is implemented and we see that any undeclared function starting with get is defined to fin the associated data , optionnally using provided arguments :

    1.  public function __call($method, $args)
    2.     {
    3.         switch (substr($method, 0, 3)) {
    4.             case ‘get’ :
    5.                 //Varien_Profiler::start(‘GETTER: ‘.get_class($this).’::’.$method);
    6.                 $key = $this->_underscore(substr($method,3));
    7.                 $data = $this->getData($key, isset($args[0]) ? $args[0] : null);
    8.                 //Varien_Profiler::stop(‘GETTER: ‘.get_class($this).’::’.$method);
    9.                 return $data;
    10.  
    11.             case ‘set’ :
    12.                 //Varien_Profiler::start(‘SETTER: ‘.get_class($this).’::’.$method);
    13.                 $key = $this->_underscore(substr($method,3));
    14.                 $result = $this->setData($key, isset($args[0]) ? $args[0] : null);
    15.                 //Varien_Profiler::stop(‘SETTER: ‘.get_class($this).’::’.$method);
    16.                 return $result;
    17.  
    18.             case ‘uns’ :
    19.                 //Varien_Profiler::start(‘UNS: ‘.get_class($this).’::’.$method);
    20.                 $key = $this->_underscore(substr($method,3));
    21.                 $result = $this->unsetData($key);
    22.                 //Varien_Profiler::stop(‘UNS: ‘.get_class($this).’::’.$method);
    23.                 return $result;
    24.  
    25.             case ‘has’ :
    26.                 //Varien_Profiler::start(‘HAS: ‘.get_class($this).’::’.$method);
    27.                 $key = $this->_underscore(substr($method,3));
    28.                 //Varien_Profiler::stop(‘HAS: ‘.get_class($this).’::’.$method);
    29.                 return isset($this->_data[$key]);
    30.         }
    31.         throw new Varien_Exception("Invalid method ".get_class($this)."::".$method."(".print_r($args,1).")");
    32.     }
    1. 12 Responses to “Magento custom attributes display on product page”

    2. May 21, 2009 - tfp

      What are the xml files that need to be edited to make it display properly?

    3. May 23, 2009 - admin

      you’re not working on xml files, but on phtml files

    4. June 10, 2009 - jg

      how do you make
      getAttributeText(’shirt_size’) ?>
      actually show up where you want it?

    5. August 17, 2009 - banjira

      Thanks a lot for this trick ! It made my day ! :)

    6. August 25, 2009 - scott

      legend, thanks for sharing this gem

    7. October 27, 2009 - slimshock

      Hello how to show the Product Attribute in onepage and in oder in admin?.
      it seems this is working in the Product Catalog but when i use it in onepage nothings happen.. i make it

      getAttributeName() ?> but no luck.. please help..

      thanks a lot..

    8. November 21, 2009 - rik

      Any idea how to add these custom attributes to a transactional email template?

    9. February 21, 2010 - Neels

      Thanks for the info – you really helped me a lot!!!

      I am just curious as to why I can call something like getWeight() or getBinding() (e.g. paperback or hardcover) in some pages and get the correct result, while in others it just returns and shows NOTHING on the rendered page?

    10. May 11, 2010 - henri odero

      what if i created an attribute which price and i named it “retailprice”. I can see in the additional information tab. How can i show it in the product list?

    11. August 3, 2010 - Rokon Boss

      Suppose you created a custom value, name ” test “.

      If you want to show the detials page on magento, then consider the following code:

      $_helper = $this->helper(‘catalog/output’);
      $_product = $this->getProduct();

      echo $_helper->productAttribute($_product, nl2br($_product->getTest()), ‘test’)

    12. December 27, 2011 - Lx web

      How about if I would like to exclude few values on front end, I know I can remove it from admin panel >> Manage Attributes >> but I actually need to show few of them on one part and few on another part so not sure how to exclude :(

      Please advise

    1. 1 Trackback(s)

    2. Apr 6, 2009: Casual Commerce » Magento Notes — How to reference custom product attributes

    Quick contact form