dpr26232 2014-09-04 20:06
浏览 26
已采纳

Magento中的客户组ID问题

I'm working with Magento 1.4.0.1, and I've found what appears to be a bug, but in fixing it I think I've created a new monster. I need some wisdom, here.

The Bug

During checkout, the quote model data (from Mage_Sales_Model_Quote) which is saved along with the cart stores two fields about the customer directly on the sales_flat_quote table: customer_tax_class_id and customer_group_id. If you examine the core quote model class you'd find a method for the former field which ensures that the tax class ID is re-loaded from DB at every request. Well, what I believe the core team failed to do (at least in this version) was also re-load the latter field: the customer group ID. By failing to re-load the group ID with each call for it, we found that the quote yielded incorrect taxes in the cart totals if the customer group changed after they had already initiated a quote in Magento.

The Fix

To solve this, I followed suit along with Mage_Sales_Model_Quote::getCustomerTaxClassId and added the following method to the quote model class:

# File: app/code/local/Mymodule/Sales/Model/Quote.php
public function getCustomerGroupId()
{
    $this->setCustomerGroupId($this->getCustomer()->getGroupId());

    return $this->getData('customer_group_id');
}

This appeared to solve my problem, so that even if the customer came back to their cart two days later, and their customer group had changed during that time, that a re-collect of totals on the quote would use their current customer group configuration. Well, on to my monster ...

The Monster

I discovered today that bundle products in the cart were showing row totals which didn't add up to the cart sub- and grand totals. After looking into it, I noticed that some of the products in the bundle had tier pricing, in this case being applied to the "General" customer group at a minimum quantity of 1.

So in tracing this thing out, I discovered that my new method above, which calls for the current customer on the quote (in this case none), was returning "General" as the customer group every time. Why? Because when I inspect the group ID getter in the customer model, I find this:

# File: app/code/core/Mage/Customer/Model/Customer.php
public function getGroupId()
{
    if (!$this->getData('group_id')) {
        $storeId = $this->getStoreId() ? $this->getStoreId() : Mage::app()->getStore()->getId();
        $this->setData('group_id', Mage::getStoreConfig(Mage_Customer_Model_Group::XML_PATH_DEFAULT_ID, $storeId));
    }
    return $this->getData('group_id');
}

And since this is an empty customer model (no ID set), it uses the default customer group from system configuration, which is in fact "General."

So what should I do? In one hand, I've solved a problem with stale customer group IDs on the quote table, and in doing so I'm forcing the quote to get a fresh ID from a customer model that's believing it's a new account.

I'm thinking I can solve this by checking if the customer is logged in when fetching the group ID, and if not, fall back to the default NOT_LOGGED_IN group ID (0 - which is what I actually want in this case, to avoid applying tier pricing). But somehow I feel like I'm tumbling down a rabbit hole while there might be a better way altogether to solve this.

Help is appreciated. Thanks!

  • 写回答

1条回答 默认 最新

  • douquan2023 2014-09-04 20:45
    关注

    OK - So I tested out my own suggestion, and it seems to work. I extended the customer model, like so:

    # File: app/code/local/Mymodule/Customer/Model/Customer.php
    public function getGroupId()
    {
        if (!$this->getData('group_id')) {
            if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
                $this->setData('group_id', Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
            } else {
                $storeId = $this->getStoreId() ? $this->getStoreId() : Mage::app()->getStore()->getId();
                $this->setData('group_id', Mage::getStoreConfig(Mage_Customer_Model_Group::XML_PATH_DEFAULT_ID, $storeId));
            }
        }
        return $this->getData('group_id');
    }
    

    Where I'm forcing the "not logged in" group when a customer session cannot be found. It solves my problem; but to be clear, I asked this because I'm afraid of making yet another change that has unforeseen effects.

    So I'm still open to better solutions!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?