異なるテーマを設定する方法はありますか?顧客グループに依存しますか?

magento.stackexchange https://magento.stackexchange.com/questions/8079

  •  16-10-2019
  •  | 
  •  

質問

さまざまなテーマが顧客グループに依存していることを示したいです。異なるテーマを動的に設定する方法はありますか?

例えば、一般的な顧客グループのテーマAと卸売顧客グループのテーマB。

前もって感謝します

役に立ちましたか?

解決

迅速で醜い方法は、現在の顧客グループをチェックしてから、テーマをプログラムで設定することです。

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帰属
所属していません magento.stackexchange
scroll top