2016-08-12 134 views
0

一個客戶有一個非常奇怪的請求:他們想要一組優惠券代碼增加一個費用一個訂單。這可能嗎?WooCommerce - 使一組優惠券添加固定費用到一個訂單

他們正在試圖解決的問題是這樣的:

目前店內提供免費送貨到48名美國,我有航運類和表費率正確配置。但是,客戶在每日交易網站上出售的優惠券應該取消免費送貨優惠並增加固定運費。

這怎麼能實現呢?

回答

4

是的,你可以用代碼段(一組優惠券)做到這一點:

function coupon_add_cart_fee($bookable_total = 0) { 
    $has_coupon = false; 

    // Set here your specials coupons slugs (one by line - last one have no coma) 
    $coupon_codes = array (
     'your_coupon_code_slug1', 
     'your_coupon_code_slug2', 
     'your_coupon_code_slug3' 
    ); 

    // Set here your fee amount or make fees calculation (see the links in reference) 
    $fee = 20; 

    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 

    // checking if that "special" coupon is applied to customer cart 
    foreach ($coupon_codes as $coupon_code) { 
     if (WC()->cart->has_discount($coupon_code)) { 

      // If yes apply the fee to the cart 
      if (!$has_coupon) { 
       $has_coupon = true; 
       break; 
      } 
     } 
    } 

    if ($has_coupon) { 
     WC()->cart->add_fee('Fees: ', $fee, false); 
    } 
} 
add_action('woocommerce_cart_calculate_fees','coupon_add_cart_fee'); 

此代碼測試,它的工作原理。它繼承您活動的兒童主題或主題的function.php文件。


稅選項與 add_fee() method

重要:稅收工作的事實或add_fee() method取決於第一的稅金設置在woocommerce。

Class WC_Cart add_fee()方法,爲購物車增加了額外的費用。

add_fee(string $name, float $amount, boolean $taxable = false, string $tax_class = '' )> <!-- language: lang-css --> 

Parameters: 
    $name  Unique name for the fee. Multiple fees of the same name cannot be added. 
    $amount Fee amount. 
    $taxable (default: false) Is the fee taxable? 
    $tax_class (default: '') The tax class for the fee if taxable. A blank string is standard tax class. 

參考:

+0

這看起來很棒,正是我所需要的,@LoicTheAztec。這可能是我缺少的一個更基本的PHP問題,但是我可以在'$ coupon_code'變量中存儲一組優惠券代碼,因爲如果正在使用許多代碼中的任何一個,則需要添加費用? – Cmagb

+0

憑藉單一優惠券價值完美測試。感謝您也幫助陣列。非常感謝您的幫助!這裏肯定會學到很多東西。 – Cmagb

相關問題