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)
    {
        switch (substr($method, 0, 3)) {
            case 'get' :
                //Varien_Profiler::start('GETTER: '.get_class($this).'::'.$method);
                $key = $this->_underscore(substr($method,3));
                $data = $this->getData($key, isset($args[0]) ? $args[0] : null);
                //Varien_Profiler::stop('GETTER: '.get_class($this).'::'.$method);
                return $data;

            case 'set' :
                //Varien_Profiler::start('SETTER: '.get_class($this).'::'.$method);
                $key = $this->_underscore(substr($method,3));
                $result = $this->setData($key, isset($args[0]) ? $args[0] : null);
                //Varien_Profiler::stop('SETTER: '.get_class($this).'::'.$method);
                return $result;

            case 'uns' :
                //Varien_Profiler::start('UNS: '.get_class($this).'::'.$method);
                $key = $this->_underscore(substr($method,3));
                $result = $this->unsetData($key);
                //Varien_Profiler::stop('UNS: '.get_class($this).'::'.$method);
                return $result;

            case 'has' :
                //Varien_Profiler::start('HAS: '.get_class($this).'::'.$method);
                $key = $this->_underscore(substr($method,3));
                //Varien_Profiler::stop('HAS: '.get_class($this).'::'.$method);
                return isset($this->_data[$key]);
        }
        throw new Varien_Exception("Invalid method ".get_class($this)."::".$method."(".print_r($args,1).")");
    }
15 replies
  1. Lx web
    Lx web says:

    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

    Reply
  2. Vishal Sanwar
    Vishal Sanwar says:

    solution of display custom attribute

    function getAttr($product_id, $attributeName) {
    $product = Mage::getModel('catalog/product')->load($product_id);
    $attributes = $product->getAttributes();
    $attributeValue = null;
    if(array_key_exists($attributeName , $attributes)){
    $attributesobj = $attributes["{$attributeName}"];
    $attributeValue = $attributesobj->getFrontend()->getValue($product);

    }
    return $attributeValue;

    }

    Reply
  3. Rokon Boss
    Rokon Boss says:

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

    Reply
  4. henri odero
    henri odero says:

    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?

    Reply
  5. Neels
    Neels says:

    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?

    Reply
  6. slimshock
    slimshock says:

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

    Reply

Trackbacks & Pingbacks

  1. [...] link here has additional information on this topic. var [...]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *