2

I have the following lines in an XML layout file to set a blank template for the product_list block (we do this to hide the product listing on some anchor categories):

<reference name="product_list">
  <action method="setTemplate">
    <template>dummy/dummy/blank.phtml</template>
  </action>
</reference>

But I only want the above code to run if ?viewall=1 is not set in the URL.

How can I implement this conditionally?

1

The requirement in the question is not achievable via layout xml file. You need to create a module to achieve this. I will show you how you can dynamically change the block template using an event observer.

You need to observe to the event controller_action_layout_generate_blocks_after. This event will get fired after every layout blocks get generated. So it is the perfect place to do customization.

Module activation file is shown below.

File : app\etc\modules\Magento_Study.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Magento_Study>
            <active>true</active>
            <codePool>local</codePool>
        </Magento_Study>
    </modules>
</config>

File : app\code\local\Magento\Study\etc\config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Magento_Study>
            <version>1.0.0</version>
        </Magento_Study>
    </modules>
    <global>
        <models>
            <magento_study>
                <class>Magento_Study_Model</class>
            </magento_study>
        </models>
    </global>
    <frontend>
        <events>
            <controller_action_layout_generate_blocks_after>
                <observers>
                    <change_product_list_template>
                        <class>magento_study/observer</class>
                        <method>changeProductListTemplate</method>
                    </change_product_list_template>
                </observers>
            </controller_action_layout_generate_blocks_after>
        </events>
    </frontend>
</config>

Here we are registering our event in the frontend section. Now define the observer.

File : app\code\local\Magento\Study\Model\Observer.php

<?php
class Magento_Study_Model_Observer
{
    const BLANK_TEMPLATE = 'dummy/dummy/blank.phtml';

    public function changeProductListTemplate(Varien_Event_Observer $observer)
    {
        //get layout object from event
        $layout = $observer->getEvent()->getLayout();

        //make sure it is a category page
        if (Mage::registry('current_category')) { 

            //check whether current url holds `?viewall=1` term in it.
            $currentUrl = Mage::helper('core/url')->getCurrentUrl();
            if (strpos($currentUrl, '?viewall=1') !== false) {

                //changing template
                $layout->getBlock('product_list')
                    ->setTemplate(self::BLANK_TEMPLATE);
            }
        }

        return $this;
    }
}

Here first we are make sureing we are in the category page. Next we are checking current url has ?viewall=1 term in it. If yes, we are dynamically changing the template. That's it.

Now clear your cache and reload the page again. Magic happens for sure :)

  • ,It will better to use Mage::app()->getRequest()->getParam('viewall') for getting` param value` – Amit Bera Sep 22 '15 at 3:46
  • @AmitBera yes you are right. I put the helper method intentionally. First you used above method in your answer. So I though showing another approach would be nice. Second my approach will work as long as the url holds ?viewall=1 irrespective of that term is a parameter or anything else. I agree the above approach is recommendable one. :) – Rajeev K Tomy Sep 22 '15 at 3:49
  • 1
    Good work. i really impressed you. :) – Amit Bera Sep 22 '15 at 3:52
1

In this,

  • create a handler on event controller_action_layout_load_before.
  • On observer create that handler on basic of viewall param value

Event: controller_action_layout_load_before

Condition: Mage::app()->getRequest()->getParam('viewall') is check page that param have value 1 or else

So , i am start an observer on basic of this condition which is add new
handler on current layout

When page with

  • then add handler viewall_page_value_is_one whenever param value is 1
  • else create handler name of viewall_page_value_is_none

Observer code:

<?php
class [ModuleNameSpace]_[ModuleName]_Model_Observer{ 
/**
     * Before load layout event handler
     *
     * @param Varien_Event_Observer $observer
     */
   public function beforeLoadLayout($observer)
{
    $Paramsvalue =  Mage::app()->getRequest()->getParam('viewall');

    if($Paramsvalue==1):
    $observer->getEvent()->getLayout()->getUpdate()
       ->addHandle('viewall_page_value_is_one');
       else:
            $observer->getEvent()->getLayout()->getUpdate()
       ->addHandle('viewall_page_value_is_none');
     endif;
}



}

Config.xml code;

  <global>
        <models>
            <[MyCustomModule_Model_Class_Groupname]>
                <class>[ModuleNameSpace]_[ModuleName]_Model</class>
            </[MyCustomModule_Model_Class_Groupname]>
        </models>
    </global>
       <frontend>
            <events>
                <controller_action_layout_load_before>
                    <observers>
                        <my_current_page_is_observer>
                            <class>[MyCustomModule_Model_Class_Groupname]/observer</class>
                            <method>beforeLoadLayout</method>
                        </my_current_page_is_observer>
                    </observers>
                </controller_action_layout_load_before>
         </events>
       </frontend>

Depends on this handler ,you can set different template product_list block

Also you can do anything layout changes .

  <viewall_page_value_is_one>
    <reference name="product_list">
        <action method="setTemplate">
            <template>dummy/dummy/blank.phtml</template>
        </action>
    </reference>
    </viewall_page_value_is_one>
  • there are some issue in code now it is fix.. – Amit Bera Sep 21 '15 at 16:08
  • Observer class name is wrong .. still.. please correct – Rajeev K Tomy Sep 21 '15 at 16:11
  • +1 for the layout handle approach. this one is recommended if someone need to apply custom template multiple times in multiple place. – Rajeev K Tomy Sep 22 '15 at 3:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.