Custom Layout Update XML on a Category in Magento

categoryxml

I have a category on my website that I am applying some Custom Layout Update to in the XML of the category. NB. I have also set "Apply To Products" to "Yes":

<reference name="content">
        <block type="core/template" name="members.header" template="members/header.phtml" before="-" />
    <remove name="product_list" />
    <block type="catalog/product_memberscontentlist" name="product_memberscontentlist" template="catalog/product/members-content-list.phtml" />
</reference>
<reference name="right">
    <block type="core/template" name="members.sidebar" template="members/sidebar.phtml" before="-" />
</reference>

I have created a file in app/code/local/Mage/Catalog/Block/Product/Memberscontentlist.php which is how the product_memberscontentlist is working. The contents of this file are:

class Mage_Catalog_Block_Product_Memberscontentlist extends Mage_Catalog_Block_Product_List
{
/**
     * Retrieve loaded category collection
     *
     * @return Mage_Eav_Model_Entity_Collection_Abstract
     */
    protected function _getProductCollection()
    {
        $collection = parent::_getProductCollection();

        if(isset($_GET['filterID'])) {
            $collection->addAttributeToSelect('my_attribute')
                ->addAttributeToFilter('my_attribute', array('like' => '%'.$_GET['filterID'].'%'));
        }

        return $collection;
    }
}

It's just a bit of code that adds an extra filter to the category list. This is working fine on the category page but is causing some kind of error on the product page which means the product page loads but for some reason the tag is 404 – Page Not Found and there is "Whoops, our bad…etc" at the bottom of the page.

My theory is that the code…

<remove name="product_list" />
<block type="catalog/product_memberscontentlist" name="product_memberscontentlist" template="catalog/product/members-content-list.phtml" />

… in the Custom Layout Update is causing the problem, so I have tried to specify that that code only be applied on the category view, ie:

<reference name="content">
    <block type="core/template" name="members.header" template="members/header.phtml" before="-" />
</reference>
<reference name="right">
    <block type="core/template" name="members.sidebar" template="members/sidebar.phtml" before="-" />
</reference>

<catalog_category_default>
    <reference name="content">
        <remove name="product_list" />
        <block type="catalog/product_memberscontentlist" name="product_memberscontentlist" template="catalog/product/members-content-list.phtml" />
    </reference>
</catalog_category_default>

But this doesn't seem to work. The problem is I need to keep "Apply to Products" to "Yes" because some of the blocks (members.header and members.sidebar) are needed on that category's product pages too.

I have also tried the following:

<reference name="category.products">
    <action method="unsetChild"><name>product_list</name></action>
    <block type="catalog/product_memberscontentlist" name="product_memberscontentlist" template="catalog/product/members-content-list.phtml" />
</reference>

But this doesn't work either!

Essentially my problem is that I want to apply Custom Layout Update to a category, and I want some (but only some) of this XML to apply to the product page too. If anyone can help with how I can target the category page only in the Custom Layout Update it would be really appreciated. Thank you!

I am using Magento 1.8.1

Best Answer

Layout handles <catalog_category_default> does not work in custom layout XML so it is not the way to go.

Instead, leave in custom layout XML field only XML which you want to apply to both products and categories.

You can put category specific layout XML in app/design/frontend/[YOUR_PACKAGE]/[YOUR_THEME]/layout/local.xml as follows:

<layout>
    <CATEGORY_18> <!-- 18 is category ID -->
        <!-- put category specific layout XML here -->
    </CATEGORY_18>
</layout>

P.S. <CATEGORY_18> layout handle is applied before custom layout XML specified in Admin Panel.