更新
您可能必须在“运输选项”选项卡下的常规运输设置中“启用调试模式”,以暂时禁用运输缓存。
以下代码将更改特定国家(此处为日本)自定义计算的运输方式成本:
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 文件中。测试和工作。
不要忘记启用回运缓存。