【问题标题】:Change the customer group according to the custom field created in the magento checkout page根据 magento 结帐页面中创建的自定义字段更改客户组
【发布时间】:2014-09-16 06:00:01
【问题描述】:

我在 magento onepage 结帐页面中创建了一个自定义字段。当用户填写此字段时,他的客户组应设置为 group 1,如果该字段留空,他的客户组应设置为 group 2

我的主要问题

1) 如何在用户通过结帐页面注册时以编程方式设置客户组值

2) 如何在结帐过程中关联我的自定义字段值和客户组值。

提前致谢。

【问题讨论】:

    标签: magento checkout


    【解决方案1】:

    要解决上述问题,您需要使用magento的事件观察者模式,他们的事件称为customer_save_before,这将帮助您获得所需的答案。

    现在,我们必须添加自定义模块。我正在调用我的模块 Customer,它是 Npm 组的一部分。因此,该文件应命名为 app/etc/modules/Npm_Customer.xml。将以下代码添加到该文件中:

    <config>
        <modules>
            <Npm_Customer>
                <active>true</active>
                <codePool>local</codePool>
            </Npm_Customer>
        </modules>
    </config>
    

    接下来,我们需要为模块创建代码。我们的代码背后的想法是,我们将在 customer_save_before 事件上创建一个观察者。 Magento 有许多我们可以观察到的事件,但大多数都超出了本文的范围。有用的是,customer_save_before 事件在创建客户和客户对其帐户进行更改时都会被调用。这意味着一个观察者将能够为这两个事件完成工作。

    我们为模块编写的所有代码都将位于 app/code/local/Npm/Customer/ 目录中。第一个文件是etc/config.xml:

    <?xml version="1.0"?>
    <config>
        <modules>
            <Npm_Customer>
                <version>1.0</version>
            </Npm_Customer>
        </modules>
        <global>
            <events>
                <customer_save_before>
                    <observers>
                        <npm_customer_save_observer>
                            <type>singleton</type>
                            <class>Npm_Customer_Model_Customer_Observer</class>
                            <method>customerSaveBefore</method>
                        </npm_customer_save_observer>
                    </observers>
                </customer_save_before>
            </events>
        </global>
    </config>
    

    下一步是创建我们刚刚告诉 Magento 我们要使用的类,并且应该在文件 Model/Customer/Observer.php 中定义:

    <?php
    class Npm_Customer_Model_Customer_Observer extends Mage_Core_Model_Abstract
    {
        /*
         * observer for the customer saved event
         */
        public function customerSaveBefore($observer)
        {
            try {
                $customer = $observer->getCustomer();
                if (null != $customer->getPermissionCode()) {
                    $customer->setData('group_id', 4); // Set the new customer group
                } else {
                    $customer->setData('group_id', 1); // Set to the default customer group
                }
            } catch ( Exception $e ) {
                Mage::log("customer_save_before observer failed: " . $e->getMessage());
            }
        }
    }
    

    如果客户从注册表单中提供许可代码,他们现在会自动分配到您的新组。

    谢谢

    【讨论】:

    • 如果客户群有不同的 tax_classes,此解决方案是否会重新计算购物车价格?
    • @Giuseppe,是的。我们只是在他注册时更改客户组,一切都将保持不变
    • 只有一个问题,如果我尝试“结帐时注册”,我没有得到任何税收的重新计算。我正在为这一点寻找解决方案,所以如果您在结帐时注册,请告诉我您是否获得了适当的税收/折扣。谢谢
    • @Giuseppe,您可以致电echo $this-&gt;helper('tax')-&gt;getPrice($_product, $_product-&gt;getPrice(), true); 并更新您的订单报价
    猜你喜欢
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多