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. 9 Responses to “Magento custom attributes display on product page”

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

    By tfp on May 21, 2009

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

    By admin on May 23, 2009

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

    By jg on Jun 10, 2009

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

    By banjira on Aug 17, 2009

  6. legend, thanks for sharing this gem

    By scott on Aug 25, 2009

  7. 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..

    By slimshock on Oct 27, 2009

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

    By rik on Nov 21, 2009

  9. 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?

    By Neels on Feb 21, 2010

  1. 1 Trackback(s)

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

Post a Comment