Magento: Numbers in Custom Attribute Codes

I ran into an issue today that I had never come across before. I have 5 custom product attributes, all with attribute codes of ‘video_1’, ‘video_2’, etc. I was trying the typical attribute output on my product view, but it wasn’t working. Thanks to my friend Moshe over at Unirgy, I now know the solution. I’ll list out some attribute codes, and a couple of the valid ways to call them (keeping in mind that this only properly works with text field and textarea attributes):

// If attribute code = 'video_one'
echo $_product->getVideoOne();
echo $_product->getVideo_one();
echo $_product->getData('video_one');
 
// If attribute code = 'video_1'
// This does NOT work:  echo $_product->getVideo1();
echo $_product->getVideo_1()
echo $_product->getData('video_1');
 
// If attribute code = 'super_1_awesome_2_sauce'
echo $_product->getSuper_1Awesome_2Sauce();
echo $_product->getSuper_1_awesome_2_sauce();
echo $_product->getData('super_1_awesome_2_sauce');
This entry was posted in Magento. Bookmark the permalink.

3 Responses to Magento: Numbers in Custom Attribute Codes

  1. The biggest reason for using the first option (in each example) as opposed to any of the others is that it gives the model/block a place to hook in and change that value before it is passed back to be used by whatever code requested the value.