這裏是你的自定義功能woocommerce_cart_calculate_fees
行動掛鉤鉤住,會做你期待什麼:
add_action('woocommerce_cart_calculate_fees','cart_conditional_discount', 10, 1);
function cart_conditional_discount($cart_object) {
if (is_admin() && ! defined('DOING_AJAX'))
return;
$cart_count = 0;
foreach($cart_object->get_cart() as $cart_item){
// Adds the quantity of each item to the count
$cart_count += $cart_item["quantity"];
}
// For 0 or 1 item
if($cart_count < 2) {
return;
}
// More than 1
else {
// Discount calculations
$modulo = $cart_count % 2;
$discount = (-$cart_count + $modulo);
// Adding the fee
$discount_text = __('Discount', 'woocommerce');
$cart_object->add_fee($discount_text, $discount, false);
}
}
代碼放在您的活動子主題(或主題)的function.php文件或也在任何插件文件中。
此代碼已經過測試並可正常工作。
我認爲'$ discount'會是'$ discount =( - $ cart_count + $ modulo)* 2;'... – Reigel
@Reigel問題不是很清楚......顯然OP想要讓一個累進折扣2 $ each 2 items ...我的代碼適用於這種情況。 – LoicTheAztec
是一致認爲它不清楚。 – Reigel