Magento: setting variables via XML "Custom Layout Updates" for each category?

I would like to be able to set variables using the "Custom Layout Updates" field, which is displayed under Manage Categories -> [Some Category] -> Custom Design , to indicate pieces of data that can be used in the list.phtml category page template.

So far I have been trying to use this:

 <reference name="product_list"> <action method="setData"> <name>custom_banner_type</name> <value>single</value> </action> <action method="setData"> <name>custom_banner_position</name> <value>3</value> </action> </reference> 

But when I try echo $this->getData("custom_banner_type"); inside list.phtml , data is not available.

I also tried to get the data inside the _beforeToHtml() function inside List.php , the template controller, but that also does nothing!

So, is there a way to pass data / variables using Custom Layout Updates XML data? This should be done there, because the data will change for each individual category, so I cannot use hard-coded files.

+5
source share
1 answer

In which layout descriptor do you do this? catalog_category_view and catalog_category_layered . You may be using the standard Magento method to set data using XML. Magento sets data in xml using the set function, which I find more readable than setData in XML.

Example:

 <catalog_category_view> <reference name="product_list"> <action method="setCustomBannerType"><value>single</value></action> <action method="setCustomBannerPosition"><value>3</value></action> </reference> </catalog_category_view> 

To retrieve data, you can use $this->getData('custom_banner_type'); as well as $this->getCustomBannerType(); which is the same. If you want to know more about this, I would suggest learning magic methods ( http://php.net/manual/en/language.oop5.magic.php )

I do not know if the cache is enabled if it clears the cache this way and it should work.

+8
source

Source: https://habr.com/ru/post/1203446/


All Articles