Расчет скидок в корзине от суммы корзины
Скидки за исключением одного товара
function woo_discount_total(WC_Cart $cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
$product_excluded_id = 4598;
$total_cart_subtotal = 0;
$discountable_subtotal = 0;
// Считаем полную сумму корзины и сумму товаров, на которые распространяется скидка
foreach ($cart->get_cart_contents() as $item) {
$line_subtotal = $item['line_subtotal'];
$total_cart_subtotal += $line_subtotal;
// На товар с исключённым ID скидка не применяется
if ($item['product_id'] != $product_excluded_id) {
$discountable_subtotal += $line_subtotal;
}
}
// Условия скидки рассчитываются от полной суммы корзины
if ($total_cart_subtotal >= 6000 && $total_cart_subtotal <= 14999) {
$discount = $discountable_subtotal * 0.08;
$cart->add_fee('Скидка 8% (от 6 000 до 15 000 ₽)', -$discount);
} elseif ($total_cart_subtotal >= 15000 && $total_cart_subtotal <= 39999) {
$discount = $discountable_subtotal * 0.13;
$cart->add_fee('Скидка 13% (от 15 000 до 40 000 ₽)', -$discount);
} elseif ($total_cart_subtotal >= 40000 && $total_cart_subtotal <= 119999) {
$discount = $discountable_subtotal * 0.14;
$cart->add_fee('Скидка 14% (от 40 000 до 120 000 ₽)', -$discount);
} elseif ($total_cart_subtotal > 120000) {
$discount = $discountable_subtotal * 0.16;
$cart->add_fee('Скидка 16% (от 120 000 ₽)', -$discount);
}
}
add_action('woocommerce_cart_calculate_fees', 'woo_discount_total');
скидка на один товар
add_action('woocommerce_cart_calculate_fees', 'custom_discount_on_product_based_on_cart_total', 20, 1);
function custom_discount_on_product_based_on_cart_total($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
if ($cart->is_empty()) return;
$product_id = 4598; // ← УКАЖИТЕ ID товара, на который нужна скидка
$discount_percent = 0;
// Получаем сумму корзины (без учета доставки и налогов)
$cart_total = $cart->get_subtotal();
// Условия по сумме корзины
if ($cart_total >= 15000) {
$discount_percent = 12;
} elseif ($cart_total >= 6000) {
$discount_percent = 8;
}
if ($discount_percent > 0) {
$discount_amount = 0;
$product_title = get_the_title($product_id);
// Проходимся по товарам в корзине
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
// Проверяем ID
if ($cart_item['product_id'] == $product_id) {
// Вычисляем скидку
$line_price = $cart_item['line_total'];
$line_discount = ($line_price * $discount_percent) / 100;
$discount_amount += $line_discount;
// ❗️Удаляем все скидки и купоны на этот товар
$cart_item['data']->set_price($cart_item['data']->get_regular_price());
}
}
if ($discount_amount > 0) {
// Добавляем пояснение к скидке
$comment = "Скидка {$discount_percent}% на \"{$product_title}\" за заказ на сумму от " .
($discount_percent === 12 ? "15 000 руб." : "6 000 руб.");
$cart->add_fee($comment, -$discount_amount);
}
}
}
// ❗️Отключаем купоны на определённый товар
add_filter('woocommerce_coupon_is_valid_for_product', 'disable_coupons_for_specific_product', 10, 4);
function disable_coupons_for_specific_product($valid, $product, $coupon, $values) {
$excluded_product_id = 1234; // ← тот же ID
if ($product->get_id() == $excluded_product_id) {
return false; // купоны не применяются
}
return $valid;
}