У меня сейчас 2 зоны доставки (Франция, Европа), и в каждой из них есть несколько вариантов доставки, включая фиксированную ставку. Эти варианты с фиксированной ставкой имеют другое уравнение для расчета комиссии в зависимости от количества товара с тремя разными классами доставки A B и C. Это достаточно просто сделать с помощью расширенных вариантов затрат, встроенных в Woocommerce.
Моя проблема в том, что я хотел бы установить минимум и максимум для платы за доставку, и эти минимумы / максимумы предназначены только для варианта с фиксированной ставкой и отличаются от географической зоны к другой. В двух словах :
- Метод фиксированной ставки во Франции:
2€ + (1€*[qtyA] + 2€*[qtyB] + 5€*[qtyC])
сMin = 7
иMax = 15
- Метод фиксированной ставки в Европе:
6€ + (2€*[qtyA] + 4€*[qtyB] + 8€*[qtyC])
сMin = 10
иMax = 25
Я попытался написать код в function.php с условиями, зависящими от зоны доставки и способа доставки, но ни минимальное, ни максимальное значение не применяются.
Надеюсь, может быть, кто-нибудь сможет помочь, поскольку это сводит меня с ума.
function maximum_shipping_rates_function( $rates, $package ) {
$methods = WC()->shipping()->get_shipping_methods();
$shipping_limit = 10.50; // I set my minimul to 10.50€
$only_apply_to_rates = array( 'shipping_method_0_flat_rate2', 'flat_rate' ); // I put here the Shipping method ID thqt I get by inspecting the option on the checkout page
// Loop through all rates
foreach ( $rates as $rate_id => $rate ) {
// Skip the shipping rates that are not in the list
if ( ! in_array( $rate_id, $only_apply_to_rates ) ) {
continue;
}
// Check if the rate is higher than my maximum
if ( $rate->cost > $shipping_limit ) {
$rate->cost = $shipping_limit;
// Recalculate shipping taxes
if ( $method = $methods[ $rate->get_method_id() ] ) {
$taxes = WC_Tax::calc_shipping_tax( $rate->cost, WC_Tax::get_shipping_tax_rates() );
$rate->set_taxes( $method->is_taxable() ? $taxes : array() );
}
}
}
return $rates;}
add_action( 'woocommerce_package_rates', 'maximum_shipping_rates_function', 10, 2 );