Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Custom Product Attribute
 
tman_f
Sr. Member
 
Total Posts:  85
Joined:  2008-02-12
 

Hi guys

I have added two custom attributes to my default product set:

image_x
image_y

I had the prenotion that I could then grab the attributes from my templates with the following:

<?php echo $_product->getImage_x() ?>

I am able to pull all the default attributes so I know my issue is with these two new ones. Does Magento add custom product attributes to the Varien_Object registry by default? Or do I have to tell the resource model to pull those too?

Or perhaps I am just mistyping this somewhere.

Appreciate the help

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
dan_w
Sr. Member
 
Total Posts:  290
Joined:  2008-01-04
 

Hello,
Are these images attributes that you are trying to grab and display? If so, I’m assuming that you chose media image as the attribute type when creating the image and will proceed under that assumption:
If setup right media images will be added as an option under images in the product page in admin and can be uploaded and assigned here as well. Assuming that you have done this you can pull this onto a product page using:
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'image_x');?>” />

You’d probably want to put a conditional before it so you don’t receive a bunch of broken link boxes in IE. This should be pretty straight forward, but if not let me know and I can walk you through it.

Dan

 
Magento Community Magento Community
Magento Community
Magento Community
 
tman_f
Sr. Member
 
Total Posts:  85
Joined:  2008-02-12
 

Ok, I will give that a shot, it might work in this scenario. But since I am crossing this bridge, I am interested to know about custom attributes in general. If I added a custom attribute that was not a sub attribute of an image, lets say I add an attribute ‘flavor’. How could I grab this attribute from my templates?

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
tman_f
Sr. Member
 
Total Posts:  85
Joined:  2008-02-12
 

Hmmm, for a test I added an new attribute named ‘test’ and placed it in the general area of the default product. After updating a sample product I have noticed that the template will not recognize this command: <?php echo $_product->getTest() ?>

That tells me that the resource model does not know to look for it, thus the product model will not register the value in the product attribute registry.

So, now the question is, does anybody know of a simple switch to tell the system to find it, or do I need to dive into the resource model and tell it to?

thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
dan_w
Sr. Member
 
Total Posts:  290
Joined:  2008-01-04
 

Thats odd. The method that you speak of has always worked for me. Are you applying the attribute to the default attribute set?
Like I said, you should be able to create any attribute and call it in a template without having to alter any of the models.

Dan

 
Magento Community Magento Community
Magento Community
Magento Community
 
dan_w
Sr. Member
 
Total Posts:  290
Joined:  2008-01-04
 

Oh, one thing I forgot to ask, what is the attribute type? Depending on the type you selected, instead of <?php echo $_product->getTest() ?>, you might need to use <?php echo $_product->getAttributeText('test') ?>. There are a couple of other methods too, again depending on type.

Dan

 
Magento Community Magento Community
Magento Community
Magento Community
 
tman_f
Sr. Member
 
Total Posts:  85
Joined:  2008-02-12
 

Yes, I have the attribute applied to all product types. Just to assure myself, i then assigned the attribute to only a simple product. Again, I checked to assure that the attribute had been assigned to the default attribute set, it is. Also, I then double checked that a simple attribute of default type had a value in my ‘test’ attribute. I notice that in the view attributes list, all attributes have the ‘system’ field set to ‘Yes’ except my additional attributes. I am not sure if that is just the difference between the Mage Core attributes and my local attributes, or if it makes a difference.

Also, I have noticed that the ‘weight’ attribute does not show up when I try to add it, as it is not included in the template by default. I feel like I keep stepping over something.

thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
tman_f
Sr. Member
 
Total Posts:  85
Joined:  2008-02-12
 

Hmm...I tried the getAttributeText() method as well, It didnt seem to work either.

Thanks Dan.

 
Magento Community Magento Community
Magento Community
Magento Community
 
tman_f
Sr. Member
 
Total Posts:  85
Joined:  2008-02-12
 

O boy! i have noticed that it shows up under the additional information of the product view page. Which means exactly what I thought, it doesnt get loaded as the rest of the attributes do, meaning I cannot request it from the Varien_Object registry.

That isn’t good enough for me, I need to be able to access these attributes as I can the others. It looks like I better get some scuba gear and prepare for a deep code dive. Does anybody (magento team) know where the method or class is that I will need to modify in order to be able to load my attributes with the others?

Any help would be appreciated.

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 

No underscores in the get*() methods.

<?php echo $_product->getImageX() ?>

The attributes only get loaded on the product view page, only certain core attributes get loaded on the *category* view page (product list).  You’ll need to specify exactly where you want the attributes to show up in order for someone to tell you how to load them.

 
Magento Community Magento Community
Magento Community
Magento Community
 
tman_f
Sr. Member
 
Total Posts:  85
Joined:  2008-02-12
 

@Mark_Kimsal : Thanks

If anyone is interested I have found a quick solution. I was looking through many Mage_Core models and I found the following:

Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect();
So I modified the product list block and works great!

Thanks guys for all your contributions.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Loxam
Member
 
Total Posts:  60
Joined:  2008-03-17
 

Could someone help me, i need to get the Weight attribute (the default magento one) displayed on the item page.

Now I have no idea where to edit to get this to show up smile

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top