Magento News

 

Magento: $_product->getData() VS. $_product->getAttributeName()

There are two main ways of displaying product attributes on the product list, and the product view. Let’s say for this example that our attribute code is ‘awesome_dude’. The two ways you’d expect to see this attribute displayed are:

<?php echo $_product->getData('awesome_dude') ?>
<?php echo $_product->getAwesomeDude() ?>

Both of these produce the same result. However, they have somewhat significant differences:

getData()

In most cases, this is the method you should use. Of the two options, this one is actually faster and more efficient. If you have no need to modify the output, and just want to display the value, use getData(). However, there are attributes that may not work properly with getData() such as the product price. Though I haven’t tested it yet, getData(‘price’) will most likely end up producing different results than getPrice().

getAttributeName()

You should really only use this method if you are needing to override the method in your block to add functionality to how the data is displayed/formatted. Magento needs to take some additional steps in rendering the data when you use this method, so it is a slightly slower process than displaying data using getData(). So, by using $_product->getAwesomeDude(), I am able to add a function to the product view block for example to modify the data before it is output to the screen:

public function getAwesomeDude()
{
  // code to do something with the data
  return $yournewdata;
}


Magento: $_product->getData() VS. $_product->getAttributeName()

Possibly Related Posts:


 

Leave a Reply