2011-06-08 34 views
4

我一直在尋找salesrule_coupon表,我發現我可以將許多優惠券代碼映射到單個規則,如果規則本身是類型的'汽車。'這非常方便,因爲我的客戶需要我們定期將代碼與數據源進行同步。加載成千上萬的代碼映射到Magento中的購物車規則

因此,在加載這些成千上萬的代碼(使用自定義模塊&直接SQL調用)時,它們加載得很好,我可以測試並驗證它們中的許多代碼是否工作。

但是,在我的這些代碼列表中,他們停止工作。第一個30左右的工作會很好,但此後,Magento說這些代碼是無效的。

我仍在調試這個,如果我發現任何東西,我會發布更新......但我已經嘗試過,現在用兩個單獨的價格規則來體驗它。第31條代碼包含一條規則,第39條代碼包含第二條。

真奇怪的是,如果我改變這些代碼指向一個不同的規則(一個代碼少於30個代碼),它們就會被識別和接受。沒有別的改變,我可以確定。

關於如何在這裏進行的任何想法?有沒有人試過這個?這很有趣。

回答

5

我在爲我的一位客戶創建類似的東西時解決了同樣的問題。檢索有效優惠券Magento核心銷售規則模塊的問題的來源使用FIND_IN_SET()GROUP_CONCAT() MySQL函數爲連接表添加附加條件。因此FIND_IN_SET只是將組連接中使用的優惠券代碼的數量截斷爲31項(32位掩碼)。另外我注意到他們使用HAVING而不是在哪裏,所以它會影響性能。

所以,你需要做的有以下幾種:

  1. 此資源模型創建重寫:Mage_SalesRule_Model_Mysql4_Rule_Collectionsalesrule/rule_collection
  2. 然後在你的資源模型,用於重寫的核心之一,您需要重新定義這個方法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; 
    } 
    
  3. 測試修正&享受Magento的發展:)

+0

有趣!是的,一個小錯誤,如果你沒有做任何替換(在'OR extra_coupon'位上),你不需要'quoteInto'。我把它放在單引號中,它效果很好。感謝您的見解! – bahoo 2011-06-09 00:11:32

4

另一種解決方案是增加你正在運行到MySQL的限制與

設置全局group_concat_max_len = 9999999;

作爲伊萬解釋FIND_IN_SET不會返回所有的優惠券代碼。您需要增加group_concat_max_len以便能夠保存由逗號(COUPON1,COUPON2,COUPON3)分隔的所有優惠券代碼的長度。

既然你可能使用不同的代碼長度不同,這可以解釋爲什麼1條規則工作了30,而其他工作了38

+0

很高興知道,儘管我擔心可能會有性能影響(更糟糕的是,最大長度是我項目中移動目標的一部分)。無論如何,我感謝幫助! – bahoo 2011-06-09 00:14:46