Magento Forum

   
not-so-newbie question: $_product->getAttributeText() not working as forum says it should. 
 
kenosis
Member
 
Total Posts:  45
Joined:  2008-03-14
 

Greetings all,

Trying REALLY hard not to ask questions already answered on the forums.  I found several posts about this topic but I can’t get either solution to work w/Mage 1.0.  I’m simply trying to read the VALUE of an attribute that’s assigned to a product.  The attribute is defined, attached to the product and I can in fact find it in the product’s attribute list (see php snipit at the bottom.) BUT, when I use the techniques Moshe described in this post http://www.magentocommerce.com/boards/viewthread/1610/P0/ I still do not get value of the attribute.  The attribute is a select list named ‘free_product’ with values “Yes” and “No” and I have tried all of the following:

$_product->getResource()->getAttribute(’free_product’)->getSource()->getOptionText($_product->getFreeproduct));
$_product->getResource()->getAttribute(’free_product’)->getSource()->getOptionText($_product->getFreeProduct));
$_product->getResource()->getAttribute(’free_product’)->getSource()->getOptionText($_product->getFree_product));
$_product->getResource()->getAttribute(’free_product’)->getSource()->getOptionText($_product->getFree_Product));

and

$_product->getAttributeText(’free_product’);

all return an empty “” result.  Any idea what I’m missing?  I read a post awhile back that mentioned that certain attribute values may not be cached and that I have to load them but I don’t know how to do that as yet.

Here’s the PHP snip that works for me EXCEPT for the attribute value returned by getAttributeText().

Thanks for any help you can lend!

Ken

<?php $_subscription = true;
foreach ($_product->getAttributes() as $attr):
if ($attr->getAttributeCode() == "free_product"):
$_subscription = false; ?>
<p> *** <?php echo $_product->getAttributeText('free_product'); ?> *** </p>
<?php break;
endif;
endforeach;
?>

 
Magento Community Magento Community
Magento Community
Magento Community
 
kenosis
Member
 
Total Posts:  45
Joined:  2008-03-14
 

Also of note: if I change the debug <p></p> element above to:

<p> *** <?php echo gettype($_product->getAttributeText('free_product')); ?> *** </p>

the value returned by getAttributeText() is boolean, which I find strange give Moshe’s posts.  But, since my attribute is a yes/no select perhaps this makes some sense, except for the fact that changing the value of the product’s attribute via the admin UI from yes to no and visa-versa does not change the return value of getAttributeText.  Hope this helps someone to help me!

Kind regards,

Ken

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

Try this - do not forget the parenthesis after $_product->getFreeProduct() as it is a method.

$_product->getResource()
    ->
getAttribute(’free_product’)
        ->
getSource()
            ->
getOptionText($_product->getFreeProduct());

 
Magento Community Magento Community
Magento Community
Magento Community
 
kenosis
Member
 
Total Posts:  45
Joined:  2008-03-14
 

Thanks a bunch Moshe , but no luck!  Changed the key line to:

<p> **** <?php echo $_product->getResource()->getAttribute('free_product')->getSource()->getOptionText($_product->getFreeProduct()); ?> ****</p>

and still get the same result.  I’m certain the change took effect because I added an extra ‘*’ delimiter and it shows but no attribute name.  SO, I went back to the admin UI, added a new text field attribute, added to the attribute set and then updated the product to give this attribute a value.  Then added your fix to the following and still get no string returned.  Is the problem perhaps the attribute internal w/an underscore in it?

<?php
foreach ($_product->getAttributes() as $attr):
if ($attr->getAttributeCode() == "package_name"): ?>
<p> **** <?php echo $_product->getResource()->getAttribute('package_name')->getSource()->getOptionText($_product->getPackageName()); ?> **** </p>
<?php break;
endif;
endforeach;
?>

 
Magento Community Magento Community
Magento Community
Magento Community
 
kenosis
Member
 
Total Posts:  45
Joined:  2008-03-14
 

Hey Moshe - so, I created a new attribute w/a simple name and get the same result.  Here’s the code snipit.  I can’t see anything I’m doing wrong.  BTW, your code fix had the attribute name in back quotes `xxx` - I can’t believe that’s what you meant and that that’s no material.  Thanks very much for your help!

<?php
foreach ($_product->getAttributes() as $attr):
if ($attr->getAttributeCode() == "package"): ?>
<p> **** <?php echo $_product->getResource()->getAttribute('package')->getSource()->getOptionText($_product->getPackage()); ?> **** </p>
<p> **** <?php echo $_product->getAttributeText('package'); ?> **** </p>
<?php break;
endif;
endforeach;
?>

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

@kenosis: let’s try this:

<?php echo $attr->getFrontend()->getValue($_product?>

 
Magento Community Magento Community
Magento Community
Magento Community
 
kenosis
Member
 
Total Posts:  45
Joined:  2008-03-14
 

Hey Moshe - well, it appears all of you original guidance was correct: I reduced my problem to the absolute basics and focused on the simple.phtml w/my custom attribute and I can now display its value!  Funny thing is that I then took the solution back to list.phtml and configurable.phtml along w/catalog.xml and it doesn’t yet work.  For the time being I’ll assume its something I’m not doing correctly.  If not, I’ll let you know.  And now that I understand things more clearly, I very much appreciate the approach.

Best,

Ken

PS. I’ll be sure to answer as many newbie questions as I can to make up for the time I took up smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
nafnaf1000
Sr. Member
 
Total Posts:  209
Joined:  2008-02-21
 

@kenosis

Did you get this working???

If you did can you please tell me what i have to do please,

(new-bie’ish)

Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
kenosis
Member
 
Total Posts:  45
Joined:  2008-03-14
 

Yes, I have this partially working and will be happy to post the steps once I work out the other 2 cases.  You can look at this post in the mean time to get a head start:  http://www.magentocommerce.com/boards/viewthread/1610/P0/ Focus on the section about how to load custom attributes (the file you need to add the action tags to is NOT main.xml, as in Moshe’s answer: you need to modify the layout file that references the template you want to display the attribute in.) More info later today (lots of meetings, sorry for the delay.)

Ken

 
Magento Community Magento Community
Magento Community
Magento Community
 
kenosis
Member
 
Total Posts:  45
Joined:  2008-03-14
 

Ok, here’s what I’ve learned about displaying custom product attributes in templates.

NOTE:

a) This explanation does not deal with organizing your changes into a custom theme, ie, the changes described are to the Mage default theme. You should really make such changes to a custom theme and how to do that can be understood by watching the Mage screencasts or reading other forum posts.

b) We do not cover the creation of attributes, attribute sets or the association of attributes with products: how to do that can be understood by watching the Mage screencasts or reading other forum posts.

1) For performance reasons, Mage does not preload all product attributes, only the ones generally needed: before you can display a custom attribute in a template you MAY have to tell Mage to load the attribute via the appropriate XML layout file.  (In one case I had to tell Mage to load the attribute and in another case I did not; and I don’t yet understand why there was a difference - perhaps the type of the attribute matters.) For example, to display an attribute in the catalog page of a SIMPLE product (using template ‘simple.phtml’), you must edit the following file: magento/app/design/frontend/default/default/layout/catalog.xml and in EVERY area where simple.phtml used you must insert an add attribute action into the effective <block> tag body.  Here, “package_name” and “free_prodct” are two of my custom attributes, e.g.,

<reference name="product.info">
<block type="catalog/product_view_type_simple" name="product.info.simple" as="product_type_data" template="catalog/product/view/type/simple.phtml">
<action method="addAttribute"><code>package_name</code></action>
<action method="addAttribute"><code>free_product</code></action>
</block>
</reference>

2) Now, edit your simple.phtml template as found in magento/app/design/frontend/default/default/template/catalog/product/view/type and all you need to do is add the following to access and display the custom attribute:  <?=$_product->getPackageName();?>

Now then, my original failure at this task was because I did not insert addAttribute actions for my custom attributes into catalog.xml.  My second failure was not to recognize that the CONFIGURABLE PRODUCT I was experimenting with did not have the attribute I thought it did: its ASSOCIATED PRODUCTS have the attribute and the Configurable Product has the associations, not the actual attribute.

This all said, the feature more or less works as Moshe described it to.  However, I have one of my 2 attributes (package_name) in two DIFFERENT attribute sets and I have two different products created one derived from each attribute set: oddly enough I can only get at the attribute value as described in step 2) for one of the two products, not the other.  I must be missing something or there could be a bug at play here.  Lastly, no matter what I have tried thus far, I cannot get at ANY of the custom attribute values from the catalog list displayed by the ‘Search Results’.  So, I have some more detective work to do, unless Moshe and company have some help to add.

Ken

 
Magento Community Magento Community
Magento Community
Magento Community
 
kenosis
Member
 
Total Posts:  45
Joined:  2008-03-14
 

Also of note:

In no case w/Mage 1.0 did the following idom work fo me:

<?php echo $_product->getResource()->getAttribute('free_product')->getSource()->getOptionText($_product->getFreeProduct()); ?>

nor did

<?php echo $attr->getAttributeText('free_product'); ?>

However, if you have the attribute you are looking for, the following DID produce the actual front-end text of the attribute:

<?php echo $attr->getFrontend()->getValue($_product) ?>

 
Magento Community Magento Community
Magento Community
Magento Community
 
nafnaf1000
Sr. Member
 
Total Posts:  209
Joined:  2008-02-21
 

@kenosis.

Thank you for the info.,....

where is the ref to <?=$_product->getPackageName();?>

to be honest. all I want to do is have a block in the Manufacturer .

what is the big deal.???

 
Magento Community Magento Community
Magento Community
Magento Community
 
kenosis
Member
 
Total Posts:  45
Joined:  2008-03-14
 

Found a small error in the loop I was using to walk all of a product’s attributes looking for those I’d added.  Now the above described solution works fine for SIMPLE products.  Hope this post helps someone else down the road to Magento happiness!

Ken

 
Magento Community Magento Community
Magento Community
Magento Community
 
kenosis
Member
 
Total Posts:  45
Joined:  2008-03-14
 

Regarding:

Thank you for the info.,....

where is the ref to <?=$_product->getPackageName();?>

to be honest. all I want to do is have a block in the Manufacturer .

what is the big deal.???

Not sure of the question:  if you want the manufacturer then I presume <?=$_product->getManufacturer();?> would work, if you added the add attribute action to the appropriate layout file, so the attribute is loaded by Mage, (Mage automatically generates these getters for product attributes, or so I understand it.)

Ken

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