2017-01-31 73 views
0

在WooCommerce中,我想在不使用優惠券的情況下打折,折扣計算將基於產品價格,例如「以3件產品的價格爲2件。購物車add_fee()在結賬頁面不起作用

function.php我的活動主題的,我使用此代碼:

function promo() { 
    if (is_cart()) { 
     $woocommerce->cart->add_fee(__('des', 'woocommerce'), -50.00`enter code here`, true, ''); 
    } 
} 
add_action ('woocommerce_cart_calculate_fees', 'promo'); 

我的問題:如果我在複習順序打折迫使這個代碼不結賬頁面上工作
。出現了,但總價值沒有變化,我認爲它沒有節省費用。

我該如何使其工作(在結帳頁面上)?

感謝

回答

1

此掛鉤的車費(或折扣)製成,所以你必須刪除if (is_cart()) {條件,就是爲什麼它不工作...

以下是實現「買2送3」折扣的正確功能代碼,可根據訂單項數量打折:

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

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

    // Initialising variable 
    $discount = 0; 

    // Iterating through cart items 
    foreach($cart_object->get_cart() as $cart_item){ 

     // Getting item data from cart object 
     $item_id = $cart_item['product_id']; // Item Id or product ID 
     $item_qty = $cart_item['quantity']; // Item Quantity 
     $product_price = $cart_item['data']->price; // Product price 
     $line_total = $cart_item['line_total']; // Price x Quantity total line item 

     // THE DISCOUNT CALCULATION 
     if($item_qty >= 3){ 
      // For each item quantity step of 3 we add 1 to $qty_discount 
      for($qty_x3 = 3, $qty_discount = 0; $qty_x3 <= $item_qty; $qty_x3 += 3, $qty_discount++); 
      $discount -= $qty_discount * $product_price; 
     } 
    } 

    // Applied discount "2 for 3" 
    if($discount != 0){ 
     // Note: Last argument is related to applying the tax (false by default) 
     $cart_object->add_fee(__('Des 2 for 3', 'woocommerce'), $discount, false); 
    } 

} 

這將簡單的產品做工,而不是產品的變化...

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

代碼已經過測試,可以正常工作。

相關問題