是否有一种方法可以根据当前客户是否属于一个组,在我的magento布局中有条件地添加一个块?
还是在控制器中更好地处理?
使用customer_logged_in
和customer_logged_out
之类的东西会很好,但遗憾的是不存在…然而。
让我们复制同样的技术。首先,你需要在config中创建一个这样的模块:
<frontend>
<events>
<controller_action_layout_load_before>
<observers>
<customer_group_observer>
<class>CUSTOM_MODULE/observer</class>
<method>beforeLoadLayout</method>
</customer_group_observer>
</observers>
</controller_action_layout_load_before>
</events>
</frontend>
在CUSTOM_MODULE_Model_Observer
类中添加以下方法:
public function beforeLoadLayout($observer)
{
$groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
$group = Mage::getModel('customer/group')->load($groupId);
$observer->getEvent()->getLayout()->getUpdate()
->addHandle('customer_group_'.$group->getCode());
}
现在在布局文件中你可以使用客户组。
<layout>
<customer_group_General>
<reference name="content">
<!-- Add some blocks -->
</reference>
</customer_group_General>
</layout>
此外,该方法不允许您直接指定每个页面的块,但您可以绕过它。下面是一个示例,仅为产品页面创建新位置,在所有其他页面上,更新应该没有效果,并且正常失败。
<layout>
<catalog_product_view>
<reference name="content">
<block type="core/text_list" name="group_container" />
</reference>
</catalog_product_view>
<customer_group_General>
<reference name="group_container">
<!-- Add some blocks -->
</reference>
</customer_group_General>
</layout>
以下文章将提供您所需功能的详细信息:http://www.magentocommerce.com/boards/viewthread/83244/#t219147
在布局文件中使用customer_logged_in或customer_logged_out块来添加或删除元素,这些块被称为last。
不需要添加额外的扩展或代码,这是内置到Magento作为标准。
希望这对我有帮助。Magento版本:1.6+