2016-12-14 79 views

回答

4

這可以使用woocommerce_cart_calculate_fees鉤子和WC_cart方法add_fee()輕鬆完成。然後,如果您使用負費用,那麼它將成爲折扣

在此功能中,折扣是從購物車小計中排除稅收(您可以輕鬆將其更改爲包括稅款在內的總計)。

下面是代碼:

add_action('woocommerce_cart_calculate_fees', 'custom_limited_discount', 10, 1); 
function custom_limited_discount($cart_object) { 

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

    // Here 20 % of discount 
    $discount_percent = 0.2; 

    // Here the max discounted amount 
    $max_discount = 500; 

    // Here are some different cart totals 
    $cart_subtotal_excl_tax = WC()->cart->subtotal_ex_tax; 
    $cart_subtotal = WC()->cart->subtotal; 
    $cart_total = WC()->cart->total; 

    $discount = 0; 

    // CALCULATION with subtotal excluding taxes 
    $calculation = $cart_subtotal_excl_tax * $discount_percent; 

    // Limiting the discount to $max_discount 
    if ($calculation > $max_discount) { 
     $discount -= $max_discount; 
    } else { 
     $discount -= $calculation; 
    } 

    $discount_text_output = __('Discount (20 %)', 'woocommerce'); 

    // Adding the discount 
    $cart_object->add_fee($discount_text_output, $discount, false); 
    // Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false) 
} 

該代碼測試,是全功能的。

代碼發送到您活動的子主題(或主題)的function.php文件中。或者也可以在任何插件php文件中使用。

注:add_fee()方法最後一個參數是與應用稅或不打折(true或false)。

+0

sir.as like我的優惠券代碼是「MAX500」,如果任何人使用優惠券代碼,那麼他們將獲得20%的折扣從500美元到2500美元,如果購物車總額超過2500美元,那麼客戶打折最多500美元。 請關注和指導我通過這個問題 –

+0

@RaviShankar對不起,但在你的問題你沒有問這個問題,所以請這個答案是與你的問題相關的好接受它...在那之後,我看到了你最新的問題這正是你在評論中對我的要求,我會盡力回答。但對於優惠券而言,情況要複雜得多,要按照您的意願製作,並且可能無法通過這種方式進行。 – LoicTheAztec