Passing Parameter Value To PHP Block From Magento CMS Pages

Friday, 10. February 2012

If you are developing a custom feature in Magento and wanted to pass a parameter/value to your PHP block class, these tips may help you greatly!

Passing Parameter Value From Inline Block Declaration

{{block type="catalog/product_list" category_id="3" template="catalog/product/list.phtml"}}

Access the parameter From you PHP script

  public function doSomethingWithPassedParameter() {
    echo $this->getCategoryId();
  }

Passing Parameter Value From Custom XML Declaration

  <reference name="content">
     <block type="catalog/product_list" name="new-arrival-products" alias="arrival_products" template="catalog/product/new_arrival.phtml">
	....
        <action method="setData"><key>category_id</key><value>6</value></action>
     </block>
  </reference>

Access the Parameter From you PHP script

  public function doSomethingWithPassedParameter() {
     echo $this->getData('category_id')
  }
Tags: , , .

Leave a Reply

You must be logged in to post a comment.