Проблема с API-интерфейсом заключается в том, что он всегда применяет налоги для отрицательного сбора (Скидка) и не заботится о существующих скидках на купоны.
В приведенном ниже коде будет установлен определенный процент скидки в самом способе доставки "Локальный самовывоз".
Вам нужно будет установить справочную стоимость доставки с простой начальной стоимостью вместо вашей формулы. Это может быть, например, 10
, и будет заменено кодом скидка.
Вам может потребоваться «Включить режим отладки» в общих настройках доставки на вкладке «Параметры доставки», чтобы временно отключить кэши доставки.
код (где вы будете устанавливать процент скидки) :
add_filter('woocommerce_package_rates', 'local_pickup_percentage_discount', 12, 2);
function local_pickup_percentage_discount( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// HERE define the discount percentage
$percentage = 5; // 5%
$subtotal = WC()->cart->get_subtotal();
// Loop through the shipping taxes array
foreach ( $rates as $rate_key => $rate ){
$has_taxes = false;
// Targetting "flat rate"
if( 'local_pickup' === $rate->method_id ){
// Add the Percentage to the label name (otional
$rates[$rate_key]->label .= ' ( - ' . $percentage . '% )';
// Get the initial cost
$initial_cost = $new_cost = $rates[$rate_key]->cost;
// Calculate new cost
$new_cost = -$subtotal * $percentage / 100;
// Set the new cost
$rates[$rate_key]->cost = $new_cost;
// Taxes rate cost (if enabled)
$taxes = [];
// Loop through the shipping taxes array (as they can be many)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $rates[$rate_key]->taxes[$key] > 0 ){
// Get the initial tax cost
$initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];
// Get the tax rate conversion
$tax_rate = $initial_tax_cost / $initial_cost;
// Set the new tax cost
$taxes[$key] = $new_cost * $tax_rate;
$has_taxes = true; // Enabling tax
}
}
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}
Код помещается в файл function.php вашей активной дочерней темы (или активной темы). Проверено и работает.
Не забудьте отключить опцию «Включить режим отладки» в настройках доставки.