Я ищу решение, которое позволит мне изменить стоимость каждой зоны доставки, потому что мне нужно иметь округленное значение также в моей второй валюте (от евро до иены для Японии). Я использовал WC_Geolocation :: geolocate_ip () для динамического изменения валюты на основе IP-адреса, но не могу найти решение для изменения стоимости зон доставки. Пример для объяснения:
$location = WC_Geolocation::geolocate_ip();
$country = $location['country'];
if($country === "JP"){
//do some code to change every shipping zone cost with a custom value
//(different for every shipping zone)
}
Я надеюсь, что я был ясен в своем объяснении.
обновленный
Возможно, вам придетсяВключить режим отладки«в общих настройках доставки»Варианты доставкивкладка, временно отключить доставку кешей.
Следующий код изменит стоимость способа доставки в пользовательских расчетах для конкретной страны. (здесь Япония):
add_filter( 'woocommerce_package_rates', 'custom_shipping_rates_based_on_country', 20, 2 );
function custom_shipping_rates_based_on_country( $rates, $package ) {
// ONLY for Japan
if( $package['destination']['country'] !== 'JP' )
return $rates;
// Loop through shipping methods rates
foreach( $rates as $rate_key => $rate ){
// Excluding free shipping method
if( $rate->method_id !== 'free_shipping') {
// Get initial shipping rate cost
$initial_cost = $rate->cost;
## ---- START ------------------------------------------- ##
// Add your calculations and settings
// Rounding decimal setting
$rounding_decimals = 2;
// Conversion rate
$conversion_rate = 1.25;
// New calculated cost
$new_cost = $initial_cost * $conversion_rate;
## ---- END ------------------------------------------- ##
// Set the rate cost (rounded with 2 decimals)
$rates[$rate_key]->cost = round( $new_cost, $rounding_decimals );
// Taxes rate cost (if enabled)
foreach ($rate->taxes as $key => $tax){
if( $rate->taxes[$key] > 0 ){
// Calculating the tax rate unit
$tax_rate = $rate->taxes[$key] / $initial_cost;
// Calculating the new tax cost
$new_tax_cost = $tax_rate * $new_cost;
// set the new tax cost
$taxes[$key] = round( $new_tax_cost, $rounding_decimals );
$has_taxes = true;
} else {
$has_taxes = false;
}
}
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}
Код помещается в файл function.php вашей активной дочерней темы (или активной темы). Проверено и работает.
Не забудьте включить кеш обратной доставки.
Других решений пока нет …