加载数千个代码以映射到 Magento 中的购物车规则

发布于 2024-11-14 22:27:57 字数 452 浏览 6 评论 0原文

我一直在查看 salesrule_coupon 表,我发现如果规则本身的类型为“自动”,我可以将许多优惠券代码映射到单个规则。这非常方便,因为我的客户需要我们定期将代码与数据源同步。

因此,在加载这数千个代码(使用自定义模块和直接 SQL 调用)时,它们加载得很好,我可以测试并验证其中许多代码是否有效。

然而,当我沿着这些代码列表工作时,它们停止工作。前 30 个左右可以正常工作,但此后,Magento 说这些代码无效。

我仍在调试这个,如果我发现任何东西,我会发布更新......但我现在已经尝试并体验了两个单独的价格规则。一条规则在第 31 号代码处失效,第二条规则在第 39 号处失效。

真正奇怪的是,如果我更改这些代码以指向不同的规则(代码少于 30 个),它们就会被识别并接受。我可以确定,没有其他改变。

关于如何继续这里的任何想法?以前有人尝试过吗?这是一件有趣的事。

I've been looking at the salesrule_coupon table, and I've discovered that I can map many coupon codes to a single rule, if the rule itself is of type 'Auto.' This is highly convenient as my client needs us to sync the codes periodically with a feed of data.

So in loading in these thousands of codes (using a custom module & direct SQL calls) they load just fine, and I can test and verify that many of them work.

However in working my way down the list of these codes, they stop working. The first 30 or so will work just fine, but thereafter, Magento says that the codes are invalid.

I'm still debugging this, and I'll post updates if I discover anything... but I've tried and experienced this with two separate price rules now. One rule crapped out at the 31st code, the second at the 39th.

What's really strange is that, if I change these codes to point to a different rule (one with less than 30 codes) they're recognized and accepted. Nothing else changed, that I can determine.

Any ideas on how to proceed here? Has anyone attempted this before? This is an interesting one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那请放手 2024-11-21 22:27:57

当我为我的一位客户创建类似的东西时,我解决了同样的问题。检索有效优惠券的问题根源 Magento Core Sales Rule 模块使用 FIND_IN_SET()GROUP_CONCAT() MySQL 函数,而不是为连接表添加附加条件。因此,FIND_IN_SET 只是将组串联中使用的优惠券代码数量截断为 31 项(32 位掩码)。我还注意到他们使用 HAVING 而不是 where,所以它会影响性能一点。

因此,您需要执行以下操作:

  1. 为此资源模型创建重写: Mage_SalesRule_Model_Mysql4_Rule_Collection (salesrule/rule_collection)
  2. 然后在重写核心模型的资源模型中,您需要重新定义此方法 setValidationFilter($websiteId, $customerGroupId, $couponCode='', $now=null) 在前端应用销售规则的限制。这是我使用的方法主体:

    <前><代码>/**
    * 修复自动优惠券验证问题
    * @todo 删除此修复,之后核心中的错误将被修复
    *
    *(非 PHPdoc)
    * @see Mage_SalesRule_Model_Mysql4_Rule_Collection::setValidationFilter()
    */
    公共函数 setValidationFilter($websiteId, $customerGroupId, $couponCode='', $now=null)
    {
    如果 (is_null($now)) {
    $now = Mage::getModel('core/date')->date('Ymd');
    }

    $this->getSelect()->where('is_active=1');
    $this->getSelect()->where('find_in_set(?, website_ids)', (int)$websiteId);
    $this->getSelect()->where('find_in_set(?, customer_group_ids)', (int)$customerGroupId);

    如果($优惠券代码){
    $couponCondition = $this->getConnection()->quoteInto(
    'extra_coupon.code = ?',
    $优惠券代码
    );

    $this->getSelect()->joinLeft(
    array('extra_coupon' => $this->getTable('salesrule/coupon')),
    'extra_coupon.rule_id = main_table.rule_id AND extra_coupon.is_primary 为 NULL AND ' 。 $优惠券条件,
    大批()
    );
    $this->getSelect()->where('('
    。 $this->getSelect()->getAdapter()->quoteInto(' main_table.coupon_type <> ?', Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC)
    。 $this->getSelect()->getAdapter()->quoteInto(' OR Primary_coupon.code = ?', $couponCode) 。 ')'
    );
    $this->getSelect()->where('('
    。 $this->getSelect()->getAdapter()->quoteInto(' main_table.coupon_type <> ?', Mage_SalesRule_Model_Rule::COUPON_TYPE_AUTO)
    。 $this->getSelect()->getAdapter()->quoteInto(' OR extra_coupon.code IS NOT NULL') 。 ')'
    );
    } 别的 {
    $this->getSelect()->where('main_table.coupon_type = ?', Mage_SalesRule_Model_Rule::COUPON_TYPE_NO_COUPON);
    }
    $this->getSelect()->where('from_date 为 null 或 from_date<=?', $now);
    $this->getSelect()->where('to_date 为 null 或 to_date>=?', $now);
    $this->getSelect()->order('sort_order');

    返回$这个;
    }

  3. Test fix &享受 Magento 开发 :)

I fixed the same issue when I was creating something similar for one of my customers. The source of the problem for retrieving of valid coupon Magento Core Sales Rule module uses FIND_IN_SET() with GROUP_CONCAT() MySQL functions instead adding additional condition for joined table. So FIND_IN_SET just truncates number of coupon codes that are used in group concatenation to 31 item (32 bits mask). Also I noticed that they are using HAVING instead of where, so it affects performance a bit.

So what you need to do are the following:

  1. Create rewrite for this resource model: Mage_SalesRule_Model_Mysql4_Rule_Collection (salesrule/rule_collection)
  2. Then in your resource model that rewrites core one, you need to redefine this method setValidationFilter($websiteId, $customerGroupId, $couponCode='', $now=null) that applies limitations for sales rules on the frontend. Here the method body that I used:

    /**
     * Fix for validation with auto-coupons
     * @todo remove this fix, after the bug in core will be fixed
     *
     * (non-PHPdoc)
     * @see Mage_SalesRule_Model_Mysql4_Rule_Collection::setValidationFilter()
     */
    public function setValidationFilter($websiteId, $customerGroupId, $couponCode='', $now=null)
    {
        if (is_null($now)) {
            $now = Mage::getModel('core/date')->date('Y-m-d');
        }
    
        $this->getSelect()->where('is_active=1');
        $this->getSelect()->where('find_in_set(?, website_ids)', (int)$websiteId);
        $this->getSelect()->where('find_in_set(?, customer_group_ids)', (int)$customerGroupId);
    
        if ($couponCode) {
            $couponCondition = $this->getConnection()->quoteInto(
                'extra_coupon.code = ?',
                $couponCode
            );
    
            $this->getSelect()->joinLeft(
                array('extra_coupon' => $this->getTable('salesrule/coupon')),
                'extra_coupon.rule_id = main_table.rule_id AND extra_coupon.is_primary IS NULL AND ' . $couponCondition,
                array()
            );
            $this->getSelect()->where('('
                . $this->getSelect()->getAdapter()->quoteInto(' main_table.coupon_type <> ?', Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC)
                . $this->getSelect()->getAdapter()->quoteInto(' OR primary_coupon.code = ?', $couponCode) . ')'
            );
            $this->getSelect()->where('('
                . $this->getSelect()->getAdapter()->quoteInto(' main_table.coupon_type <> ?', Mage_SalesRule_Model_Rule::COUPON_TYPE_AUTO)
                . $this->getSelect()->getAdapter()->quoteInto(' OR extra_coupon.code IS NOT NULL') . ')'
            );
        } else {
            $this->getSelect()->where('main_table.coupon_type = ?', Mage_SalesRule_Model_Rule::COUPON_TYPE_NO_COUPON);
        }
        $this->getSelect()->where('from_date is null or from_date<=?', $now);
        $this->getSelect()->where('to_date is null or to_date>=?', $now);
        $this->getSelect()->order('sort_order');
    
        return $this;
    }
    
  3. Test fix & Enjoy Magento Development :)

任谁 2024-11-21 22:27:57

另一个解决方案是增加您遇到的 mysql 限制

SET GLOBAL group_concat_max_len=9999999;

正如 Ivan 所解释的那样,FIND_IN_SET 不会返回您的所有优惠券代码。您需要增加 group_concat_max_len 才能容纳所有以逗号分隔的优惠券代码的长度(COUPON1、COUPON2、COUPON3)。

由于您可能使用了不同长度的不同代码,这可以解释为什么 1 条规则适用于 30,而另一个规则适用于 38。

Another solution is to increase the mysql limit you are running into with

SET GLOBAL group_concat_max_len=9999999;

as Ivan explained the FIND_IN_SET does not return all your coupon codes. You need to increase group_concat_max_len to be able to hold the length of all your coupon codes delimited by a comma (COUPON1, COUPON2, COUPON3).

Since you have likely used different codes with different lengths, this would explain why 1 rule worked for 30 while the other worked for 38.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文