Magento:如果类别没有产品,则更改布局

时间:2014-07-08 10:30:54

标签: magento layout

当类别为空时,我想将类别页面的2列布局更改为1列。由于我使用左列进行分层导航,并且在这种情况下没有任何一个,左列显示为空白并且看起来不太好。

有任何建议吗?

谢谢!

2 个答案:

答案 0 :(得分:3)

在app / code / local / Amit / Custommodule下创建一个模块 并在app / code / local / Amit / Custommodule / etc / code和code

下创建config.xml
<?xml version="1.0"?>
<config>
    <modules>
        <Amit_Custommodule>
            <version>1.0.0.0</version>
        </Amit_Custommodule>
    </modules>
    <global>
        <models>
            <amitcustommodule>
                <class>Amit_Custommodule_Model</class>
            </amitcustommodule>
        </models>
    </global>
    <frontend>

        <events>
            <controller_action_predispatch_catalog_category_default>
                <observers>
                    <amitcustommodule_default>
                        <class>amitcustommodule/observer</class>
                        <method>logCartAdd</method>
                    </amitcustommodule_default>
                </observers>
            </controller_action_predispatch_catalog_category_default>
                <controller_action_predispatch_catalog_category_layered>
                <observers>
                    <amitcustommodule_default_2>
                        <class>amitcustommodule/observer</class>
                        <method>logCartAdd</method>
                    </amitcustommodule_default_2>
                </observers>
            </controller_action_predispatch_catalog_category_layered>
    </events>

    </frontend>
</config>

Observer.php到app / code / local / Amit / Custommodule / Model /

 <?php Amit_Custommodule_Model_Observer
 public function logCartAdd(Varien_Event_Observer $observer)
    {

    $_productCollection=$observer->getEvent()
       ->getLayout()->getBlock('product_list')->getLoadedProductCollection();
     if(!$_productCollection->count()): 
    $observer->getEvent()->getLayout()
                       ->getBlock('root')
                       ->setTemplate('page/1column.phtml');  
    endif;
}

答案 1 :(得分:2)

我偶然发现了这一点,因为我试图做同样的事情。不幸的是,这个解决方案不起作用,但它指出了我正确的方向。最终的解决方案是:

<?xml version="1.0"?>
<config>
<modules>
    <Branded3_Emptycategory>
        <version>0.1.0</version>
    </Branded3_Emptycategory>
</modules>
<global>
    <models>
        <branded3_emptycategory>
            <class>Branded3_Emptycategory_Model</class>
            <resourceModel>branded3_emptycategory_resource</resourceModel>
        </branded3_emptycategory>
        <branded3_emptycategory_resource>
            <class>Branded3_Emptycategory_Model_Resource</class>
        </branded3_emptycategory_resource>
    </models>
    <blocks>
        <branded3_emptycategory>
            <class>Branded3_Emptycategory_Block</class>
        </branded3_emptycategory>
    </blocks>
    <helpers>
        <branded3_emptycategory>
            <class>Branded3_Emptycategory_Helper</class>
        </branded3_emptycategory>
    </helpers>
</global>

<frontend>
    <events>
        <controller_action_layout_generate_blocks_after>
            <observers>
                <branded3_default>
                    <class>branded3_emptycategory/observer</class>
                    <method>zeroproducts</method>
                </branded3_default>
            </observers>
        </controller_action_layout_generate_blocks_after>
    </events>
</frontend>
</config>

观察员:

class Branded3_Emptycategory_Model_Observer {

public function zeroproducts(Varien_Event_Observer $observer)
{
    $controller   = $observer->getAction();

    //limit to the product view page
    if ($controller->getFullActionName() != 'catalog_category_view')
    {
        return;
    }

    $_productCollection = $observer->getEvent()
        ->getLayout()->getBlock('product_list')->getLoadedProductCollection();

    $products = clone $_productCollection;

    if(!$products->count()):
        $observer->getEvent()->getLayout()
                 ->getBlock('root')
                 ->setTemplate('page/1column.phtml');
    endif;

}

}