Magento 1.9 – Displaying Attribute on Category Page with system.log Error

attributesfrontendmagento-1.9product-attribute

I think I'm doing something wrong, and I'd appreciate any insight.

I have an attribute for my products called 'freeshipping'

It's either set to Yes or No for each product.

On my category pages, I use the attribute to display a Free shipping icon with each product with this code:

<?php if ($_helper->productAttribute($_product, $_product->getAttributeText('freeshipping')) == "Yes"):?><img src="<?php echo $this->getSkinUrl('images/shipsfree-bug.png') ?>" class="freeshipicon" />

It's working fine on the frontend: it shows the free shipping icon if the the product has free shipping, and if it doesn't it shows nothing. The problem is that it's filling my system.log with this error:

2014-11-20T13:35:59+00:00 ERR (3): Warning: Missing argument 3 for Mage_Catalog_Helper_Output::productAttribute(), called in /MYPATHTOMAGENTO/app/design/frontend/rwd/default/template/catalog/product/list.phtml on line 150 and defined  in /MYPATHTOMAGENTO/app/code/core/Mage/Catalog/Helper/Output.php on line 120
2014-11-20T13:35:59+00:00 ERR (3): Notice: Undefined variable: attributeName  in /MYPATHTOMAGENTO/app/code/core/Mage/Catalog/Helper/Output.php on line 122
2014-11-20T13:35:59+00:00 ERR (3): Notice: Undefined variable: attributeName  in /MYPATHTOMAGENTO/app/code/core/Mage/Catalog/Helper/Output.php on line 141

So clearly I'm doing something wrong in /template/catalog/product/list.phtml with the code above when I'm trying to check to see if the product has the freeshipping attribute set to Yes.

Any help would be greatly appreciate. Thanks in advance.

Best Answer

This is not an answer to your question, but I might be a solution to your problem.
Don't use

$_helper->productAttribute($_product, $_product->getAttributeText('freeshipping')) == "Yes"

This is an ugly way of checking a value and it falls over if you change the language. For example for French, with the French language pack installed this will evaluate always to false because the result will be either "Oui" or "Non".

The getAttributeText method should be used when you have dropdown attributes with a lot of options. For yes/no attributes is clear. 1 = Yes, 0 = No.

Also the $_helper->productAttribute has no value here. that method just parses and interprets {{...}} directives from attributes that support HTML.
it also dispatches an event that let's you manipulate the value of the attribute. But I doubt you need to manipulate the value of a yes/no attribute.

But enough with the explanations. Instead of the if statement you use, try this

<?php if ($_product->getData('freeshipping')):?>
    <img src="<?php echo $this->getSkinUrl('images/shipsfree-bug.png') ?>" class="freeshipicon" />
<?php endif?>
Related Topic