Hosted applications, Tips & Tricks, Web Components
- March 11, 2009
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 :
-
public function __call($method, $args)
-
{
-
case ‘get’ :
-
//Varien_Profiler::start(’GETTER: ‘.get_class($this).’::’.$method);
-
//Varien_Profiler::stop(’GETTER: ‘.get_class($this).’::’.$method);
-
return $data;
-
-
case ’set’ :
-
//Varien_Profiler::start(’SETTER: ‘.get_class($this).’::’.$method);
-
//Varien_Profiler::stop(’SETTER: ‘.get_class($this).’::’.$method);
-
return $result;
-
-
case ‘uns’ :
-
//Varien_Profiler::start(’UNS: ‘.get_class($this).’::’.$method);
-
$result = $this->unsetData($key);
-
//Varien_Profiler::stop(’UNS: ‘.get_class($this).’::’.$method);
-
return $result;
-
-
case ‘has’ :
-
//Varien_Profiler::start(’HAS: ‘.get_class($this).’::’.$method);
-
//Varien_Profiler::stop(’HAS: ‘.get_class($this).’::’.$method);
-
}
-
}



10 Responses to “Magento custom attributes display on product page”
What are the xml files that need to be edited to make it display properly?
By tfp on May 21, 2009
you’re not working on xml files, but on phtml files
By admin on May 23, 2009
how do you make
getAttributeText(’shirt_size’) ?>
actually show up where you want it?
By jg on Jun 10, 2009
Thanks a lot for this trick ! It made my day !
By banjira on Aug 17, 2009
legend, thanks for sharing this gem
By scott on Aug 25, 2009
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
Any idea how to add these custom attributes to a transactional email template?
By rik on Nov 21, 2009
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
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?
By henri odero on May 11, 2010