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!

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题