我想显示不同的主题取决于客户群。是否有任何方法可以动态设置不同的主题?

例如:通用客户组和主题B的主题A批发客户组。

提前致谢

有帮助吗?

解决方案

快速而丑陋的方法是检查当前客户的组,然后以编程方式设置主题:

Mage::getDesign()->setArea('frontend')
    ->setPackageName('your_package')
    ->setTheme('your_theme');

但是,该解决方案肯定会错过一些灵活性。

更复杂的方法是创建客户组布局汉德尔,然后在此设置自定义主题。该解决方案灵感来自 本文 由Atwix。

所以首先你必须观察 controller_action_layout_load_before 事件:

<events>
    <controller_action_layout_load_before>
        <observers>
            <customer_group_handle>
                <class>module/observer</class>
                <method>addCustomerGroupHandle</method>
            </customer_group_handle>
        </observers>
    </controller_action_layout_load_before>
</events>

然后在您的观察者类中 addCustomerGroupHandle 方法:

public function addCustomerGroupHandle(Varien_Event_Observer $observer)
{
    if (Mage::helper('customer')->isLoggedIn()) {
        /** @var $update Mage_Core_Model_Layout_Update */
        $update = $observer->getEvent()->getLayout()->getUpdate();
        $groupId = Mage::helper('customer')->getCustomer()->getGroupId();
        $groupName = Mage::getModel('customer/group')->load($groupId)->getCode();
        $update->addHandle('customer_group_' . str_replace(' ', '_', strtolower($groupName)));
    }

    return $this;
}

注意: str_replace 这不是万无一失的,所以我建议用正则表达式代替它,该表达式将用下划线替换所有非字母数字的字符,然后将带领和尾随下划线替换。

因此,现在您可以通过XML为任何客户组设置自定义主题:

<?xml version="1.0" encoding="UTF-8"?>
<layout>
    <customer_group_wholesale>
        <reference name=”root”>
            <action method=”setTheme”><theme>modern</theme></action>
        </reference>
    </customer_group_wholesale>
</layout>

其他提示

这更多是蒂姆答案的补充。您可以为每个客户组创建配置部分,并为其中的主题设置值。这样,您就不必硬编码客户组名称,也不必每次添加新组时都更改代码。
这是如何添加动态配置字段的示例. 。它涉及为配置部分组创建新的渲染器。
将观察者与蒂姆的建议结合在一起:

Mage::getDesign()->setArea('frontend')
    ->setPackageName('your_package')
    ->setTheme('your_theme');

应该得到您期望的结果。

许可以下: CC-BY-SA归因
scroll top