2017-04-04 32 views
2

is_valid功能核心代碼如何刪除is_valid功能$this->validate_product_categories();在WooCommerce class-wc-coupon.php文件,而無需編輯插件?旁路validate_product_categories()在類WC_Coupon

在functions.php中有這樣做的鉤子嗎?

這裏是class-wc-coupon.php的源代碼:

/** 
    * Check if a coupon is valid. 
    * 
    * @return boolean validity 
    * @throws Exception 
    */ 
    public function is_valid() { 
     try { 
      $this->validate_exists(); 
      $this->validate_usage_limit(); 
      $this->validate_user_usage_limit(); 
      $this->validate_expiry_date(); 
      $this->validate_minimum_amount(); 
      $this->validate_maximum_amount(); 
      $this->validate_product_ids(); 
      $this->validate_product_categories(); 
      $this->validate_sale_items(); 
      $this->validate_excluded_items(); 
      $this->validate_cart_excluded_items(); 

      if (! apply_filters('woocommerce_coupon_is_valid', true, $this)) { 
       throw new Exception(self::E_WC_COUPON_INVALID_FILTERED); 
      } 
     } catch (Exception $e) { 
      echo $this->error_message = $this->get_coupon_error($e->getMessage()); 
      return false; 
     } 

     return true; 
    } 

感謝

+0

爲什麼要刪除它?你可以更新插件,並檢查是否有任何問題,並可以聯繫支持論壇的問題。強烈推薦。 –

+0

回到你的問題的解決方案,在你的* functions.php *我想你需要覆蓋這個功能。像這樣的http://hookr.io/filters/woocommerce_coupon_is_valid/不知道。 –

+0

也檢查這一次http://stackoverflow.com/questions/21397325/want-to-overwrite-functions-written-in-woocommerce-functions-php-file –

回答

1

更新

這是一個工作的測試代碼(不用於類優惠券限制),所以你會必須從您的優惠券中刪除這些類別設置。此代碼將檢查和刪除優惠券,如果它不符合產品編號添加到購物車(因爲你只有6種產品在2類)。

這裏是代碼:

add_action('woocommerce_before_calculate_totals', 'custom_check_coupons', 10, 1); 
function custom_check_coupons($cart_obj) { 
    $coupons_in_cart = $cart_obj->get_applied_coupons(); 

    if(! empty($coupons_in_cart)){ 

     // Set below your coupon slugs and your corresponding product IDs 
     $coupon1 = 'couponslug1'; // Coupon group 1 
     $product_ids_1 = array(56, 53, 50); // Product Ids for coupon group 1 
     $coupon2 = 'couponslug2'; // Coupon group 2 
     $product_ids_2 = array(24, 38, 44); // Product Ids for coupon group 2 

     $coupon_match = false; 

     foreach($cart_obj->get_cart() as $cart_item_key => $cart_item_values) { 
      $product_id = $cart_item_values['product_id']; 
      if (in_array($product_id, $product_ids_1)){ 
       if(in_array($coupon1, $coupons_in_cart)){ 
        $coupon_match = true; 
       } 
      } elseif (in_array($product_id, $product_ids_2)){ 
       if(in_array($coupon2, $coupons_in_cart)){ 
        $coupon_match = true; 
       } 
      } 
     } 
     if(! $coupon_match){ 
      foreach($coupons_in_cart as $coupon){ 
       $cart_obj->remove_coupons($coupon); 
       break; 
      } 
      // (optional) displaying a notice 
      wc_add_notice(__('The coupon "'.$coupon.'" can’t be used and has been removed', 'woocoommerce'), 'error'); 
     } 
    } 
} 

的代碼放在你的活躍兒童主題(或主題)的function.php文件或也以任何插件文件。


你應該嘗試使用woocommerce_coupon_is_valid過濾鉤子鉤住這個自定義函數,除去優惠券驗證爲$this->validate_product_categories()情況:

add_filter('woocommerce_coupon_is_valid', 'remove_product_cat_coupon_validation', 1, 2); 
function remove_product_cat_coupon_validation($valid, $coupon){ 
    if(! $coupon->validate_product_categories()) $valid = true; 
} 

的代碼放在function.php文件您的活動孩子主題(或主題)或任何插件文件中。

此代碼應該工作。

+0

它不是爲我工作 – sarun

+0

不適合我的工作我想刪除從私有函數validate_product_categories)拋出新的異常(如果(!$ valid_for_cart){ \t \t \t \t拋出新的異常( self :: E_WC_COUPON_NOT_APPLICABLE); \t \t \t} – sarun