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




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