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

加载数千个代码以映射到Magento中的购物车规则,magento,coupon,Magento,Coupon,我一直在查看salesrule\u优惠券表,发现如果规则本身为“Auto”类型,我可以将许多优惠券代码映射到单个规则。这非常方便,因为我的客户需要我们定期将代码与数据源同步 因此,在加载这数千个代码(使用自定义模块&直接SQL调用)时,它们加载得很好,我可以测试和验证其中许多代码是否正常工作 然而,当我沿着这些代码列表往下看时,它们停止了工作。前30个左右就可以了,但此后,Magento说这些代码是无效的 我还在调试这个,如果我发现什么,我会发布更新。。。但我现在已经尝试并体验了两种不同的价格规

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

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

然而,当我沿着这些代码列表往下看时,它们停止了工作。前30个左右就可以了,但此后,Magento说这些代码是无效的

我还在调试这个,如果我发现什么,我会发布更新。。。但我现在已经尝试并体验了两种不同的价格规则。有一条规则在第31码处失效,第二条规则在第39码处失效

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


你对如何继续下去有什么想法吗?以前有人尝试过吗?这是一个有趣的问题。

我在为我的一位客户创建类似产品时解决了同样的问题。检索有效优惠券Magento核心销售规则模块的问题源使用
FIND_IN_SET()。所以
FIND_IN_SET
只是将组连接中使用的优惠券代码的数量截断为31个项目(32位掩码)。我还注意到他们使用的是have而不是where,所以这会稍微影响性能

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

  • 为此资源模型创建重写:
    Mage\u SalesRule\u model\u Mysql4\u Rule\u Collection
    SalesRule/Rule\u Collection
  • 然后,在重写core one的资源模型中,您需要重新定义此方法
    setValidationFilter($websiteId、$customerGroupId、$couponCode='',$now=null)
    ,该方法对前端的销售规则应用限制。这里是我使用的方法体:

    /**
     * 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;
    }
    
    /**
    *使用自动优惠券进行验证的修复
    *@todo删除此修复,修复核心中的错误后
    *
    *(非PHPdoc)
    *@请参阅Mage_SalesRule_Model_Mysql4_Rule_Collection::setValidationFilter()
    */
    公共函数setValidationFilter($websiteId、$customerGroupId、$couponCode='',$now=null)
    {
    如果(为空($now)){
    $now=Mage::getModel('core/date')->date('Y-m-d');
    }
    $this->getSelect()->其中('is_active=1');
    $this->getSelect()->其中('find_in_set(?,website_id)'(int)$websiteId);
    $this->getSelect()->其中('find_in_set(?,customer_group_id)'(int)$customerGroupId);
    如果($couponCode){
    $couponCondition=$this->getConnection()->quoteInto(
    '额外优惠券代码=?',
    $couponCode
    );
    $this->getSelect()->joinLeft(
    数组('extra_优惠券'=>$this->getTable('salesrule/优惠券'),
    “extra_优惠券.rule_id=main_table.rule_id和extra_优惠券.is_primary为空,”$couponCondition,
    数组()
    );
    $this->getSelect()->其中('('
    .$this->getSelect()->getAdapter()->quoteInto('main\u table.优惠券类型?',Mage\u SalesRule\u Model\u Rule::优惠券类型特定)
    .$this->getSelect()->getAdapter()->quoteInto('OR primary_.code=?',$couponCode)。'
    );
    $this->getSelect()->其中('('
    .$this->getSelect()->getAdapter()->quoteInto('main_table.优惠券类型?',Mage_SalesRule_Model_Rule::优惠券类型自动)
    .$this->getSelect()->getAdapter()->quoteInto('或额外的优惠券代码不为NULL')。'
    );
    }否则{
    $this->getSelect()->其中('main\u table.优惠券类型=?',Mage\u SalesRule\u Model\u Rule::优惠券类型\u NO\u优惠券);
    }
    $this->getSelect()->其中('from_date为空或from_date=?',$now);
    $this->getSelect()->order('sort_order');
    退还$this;
    }
    
  • 测试修复和享受Magento开发:)


  • 另一个解决方案是增加mysql的限制

    设置全局组\u concat\u max\u len=999999

    正如Ivan所解释的,FIND_IN_集合不会返回您所有的优惠券代码。您需要增加group_concat_max_len,以便能够容纳所有优惠券代码的长度,这些代码由逗号分隔(COUPON1、COUPON2、COUPON3)


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

    有趣!是的,一个小错误,如果你不做任何替换(在
    或额外优惠券上),你不需要
    引用到
    中。我把它放在单引号里,效果很好。谢谢你的见解!很高兴知道,尽管我担心可能会影响性能(更糟糕的是,最大长度在我的项目中是一个移动的目标)。无论如何,我感谢你的帮助!