Magento:如何按代码更改目录产品布局视图?

Magento:如何按代码更改目录产品布局视图?,magento,Magento,我的目录产品使用3列布局。右侧的分层导航将产品置于中间,左侧的购物车置于中间。我的问题是,当我的产品类别中没有产品时,右边的列为空。我希望当我的类别为空时,ma目录产品从3列转换为2列。我可以通过代码来完成吗?使用以下代码: $product->setPageLayout('two_columns_right')//两列左列或一列您可以在其中编写自己的模块(我将其命名为“ProductTemplate”),您可以使用事件: <frontend> <events>

我的目录产品使用3列布局。右侧的分层导航将产品置于中间,左侧的购物车置于中间。我的问题是,当我的产品类别中没有产品时,右边的列为空。我希望当我的类别为空时,ma目录产品从3列转换为2列。我可以通过代码来完成吗?

使用以下代码:


$product->setPageLayout('two_columns_right')//两列左列或一列

您可以在其中编写自己的模块(我将其命名为“ProductTemplate”),您可以使用事件:

<frontend>
    <events>
        <catalog_product_load_before>
            <observers>
                <yournamespace_producttemplate>
                    <type>singleton</type>
                    <class>yournamespace_producttemplate/observer</class>
                    <method>catalogProductLoadBefore</method>
                </yournamespace_producttemplate>
            </observers>
        </catalog_product_load_before>
    </events>
</frontend>
class Yournamespace_ProductTemplate_Model_Observer {

  public function catalogProductLoadBefore (Varien_Event_Observer $observer) {
    $product = $observer->getProduct();
    $product->setPageLayout('two_columns_left');
  }
}