I need help familiarizing myself with helpers, their methods, and product attributes. Specifically: $_helper->productAttribute($product, $attributeHtml, $attributeName)
Here are the files I am using/reviewing:
app\code\core\Mage\catalog\helper\output.php
app\design\frontend\[theme name]\template\catalog\product\view\media.phtml
The following line of code generates the html for the product image.
echo $_helper->productAttribute($_product, $_img, 'image');
The helper class code describes the method in the following pieces of code. What is being returned, what are the steps, and why would I use this method instead of simply echoing the img html described in a previous line of the template file?
public function getHandlers($method)
{
$method = strtolower($method);
return isset($this->_handlers[$method]) ? $this->_handlers[$method] : array();
}
public function process($method, $result, $params)
{
foreach ($this->getHandlers($method) as $handler) {
if (method_exists($handler, $method)) {
$result = $handler->$method($this, $result, $params);
}
}
return $result;
}
public function productAttribute($product, $attributeHtml, $attributeName)
{
/* Code that is not relevant to this example */
$attributeHtml = $this->process('productAttribute', $attributeHtml, array(
'product' => $product,
'attribute' => $attributeName
));
return $attributeHtml;
}
A product attribute is a characteristic that defines a particular product and will affect a consumer's purchase decision. Product attributes can be tangible (or physical in nature) or intangible (or not physical in nature).
Go to: Products > Add Product (or edit an existing one). Select the Attributes tab in the Product Data. There you can choose any of the attributes that you've created in the dropdown menu. Select Add.
Very nice question!
So actually a bit about purpose of this helper. From its name you already can make a conclusion that it is used for output of the data. Method name is also self explanatory, it just it outputs product attribute value depends on handlers. There are currently two methods, productAttribute()
, that is used for outputting product attribute values and categoryAttribute()
, that is used for category ones. All data in core templates from category and product is outputted though this method (except price attribute), as far as I remember it was added in one of 1.4.x version, but not sure. The main idea was to make possible filtering the data of the attribute. For instance you can use {{widget ... }}
constructions in category description, it is realized via special methods.
Both this methods actually does the same functionality, but for different entities. Both of them receiving 3 arguments:
First inside of this methods, Magento checks allowance of html markup in the value, if not, it escapes text with escapeHtml()
method. Also if attribute has a textarea as input in the admin, all new line characters are replaced with <br />
tag.
If html is allowed, Magento checks for allowance of special constructions like {{widget ...}}
in configuration (official name of this constructions is directive). If directives are allowed, special directive processor gets instantiated and value gets processed.
After all core processing is done, Magento invokes handlers.
This handlers are additional functionality that is not used by core modules, but you can use your own customization to achieve some nice customizations. Here is example: You'd like to make all the output of product name in uppercase. Then you can add your own handler, for this purpose follow this simple steps:
Define an observer for catalog_helper_output_construct
<config>
<frontend>
<events>
<catalog_helper_output_construct>
<observers>
<your_module>
<class>your_module/observer</class>
<method>handleHelperOutputInitialization</method>
</your_module>
</observers>
</catalog_helper_output_construct>
</events>
</frontend>
</config>
Create your observer class, also I will make it as handler as well. The code is very simple:
class Your_Module_Model_Observer
{
public function handleHelperOutputInitialization($observer)
{
$helper = $observer->getEvent()->getHelper();
$helper->addHandler('productAttribute', $this);
}
public function productAttribute($helper, $value, $parameters)
{
$attribute = $parameters['attribute'];
if ($attribute->getAttributeCode() == 'name') {
return strtoupper($value);
}
return $value;
}
}
Make sure that your method name in handler class is absolutely the same as method name of value processor, in this example it is productAttribute()
.
Enjoy learning Magento!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With