问题描述
我目前有 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 );
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)