在 WooCommerce 中按运输区域设置最低和最高运费成本
Set a min and max shipping rate cost by shipping zone in WooCommerce
我目前有 2 个送货区(法国、欧洲),每个送货区都有多种送货选项,包括固定费率。这些固定费率选项有不同的公式来计算基于 A B 和 C 三种不同运费的项目数量的费用 class。使用 Woocommerce 原生的高级成本选项很容易。
我的问题是我想为运费设置最低和最高限额,这些最低/最高限额仅适用于统一费率选项,并且因地理区域而异。简而言之:
- 法国统一费率法:
2€ + (1€*[qtyA] + 2€*[qtyB] + 5€*[qtyC])
with Min = 7
and 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 );
要按送货区名称设置不同的最低和最高运费,请改用以下内容:
add_filter( 'woocommerce_package_rates', 'specificproductsshipping_methods', 10, 2 );
function specificproductsshipping_methods( $rates, $package ){
$first_rate = reset($rates); // get first rate (to get the shipping zone from)
$chosen_zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $first_rate->instance_id ); // Current Shipping Zone Object
$zone_name = $chosen_zone->get_zone_name(); // Get shipping zone name
// Loop through all rates
foreach ( $rates as $rate_key => $rate ) {
// Targeting "Flat rate" shipping methods only
if ( 'flat_rate' === $rate->method_id ) {
$initial_cost = $new_cost = $rate->cost;
// 1. For "France" shipping zone name (set min and max cost)
if ( 'France' === $zone_name ) {
if ( $initial_cost < 7 ) {
$new_cost = 7;
} elseif ( $initial_cost > 15 ) {
$new_cost = 15;
}
}
// For "Europe" shipping zone name (set min and max cost)
elseif ( 'Europe' === $zone_name ) {
if ( $initial_cost < 10 ) {
$new_cost = 10;
} elseif ( $initial_cost > 25 ) {
$new_cost = 25;
}
}
if ( $new_cost != $initial_cost ) {
$rates[$rate_key]->cost = $new_cost; // Set the new cost
$taxes = []; // Initializing
// Taxes: Loop through the shipping taxes array (as they can be many)
foreach ($rate->taxes as $key => $tax){
if( $tax > 0 ){
$initial_tax_cost = $tax; // Get the initial tax cost
$tax_rate = $initial_tax_cost / $initial_cost; // Get the tax rate conversion
$taxes[$key] = $new_cost * $tax_rate; // Set the new tax cost in taxes costs array
$has_taxes = true; // Enabling tax
}
}
if( isset($has_taxes) && $has_taxes ) {
$rates[$rate_key]->taxes = $taxes; // Set taxes costs array
}
}
}
}
return $rates;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
Don't forget to empty your cart to refresh shipping method caches.
我目前有 2 个送货区(法国、欧洲),每个送货区都有多种送货选项,包括固定费率。这些固定费率选项有不同的公式来计算基于 A B 和 C 三种不同运费的项目数量的费用 class。使用 Woocommerce 原生的高级成本选项很容易。
我的问题是我想为运费设置最低和最高限额,这些最低/最高限额仅适用于统一费率选项,并且因地理区域而异。简而言之:
- 法国统一费率法:
2€ + (1€*[qtyA] + 2€*[qtyB] + 5€*[qtyC])
withMin = 7
andMax = 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 );
要按送货区名称设置不同的最低和最高运费,请改用以下内容:
add_filter( 'woocommerce_package_rates', 'specificproductsshipping_methods', 10, 2 );
function specificproductsshipping_methods( $rates, $package ){
$first_rate = reset($rates); // get first rate (to get the shipping zone from)
$chosen_zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $first_rate->instance_id ); // Current Shipping Zone Object
$zone_name = $chosen_zone->get_zone_name(); // Get shipping zone name
// Loop through all rates
foreach ( $rates as $rate_key => $rate ) {
// Targeting "Flat rate" shipping methods only
if ( 'flat_rate' === $rate->method_id ) {
$initial_cost = $new_cost = $rate->cost;
// 1. For "France" shipping zone name (set min and max cost)
if ( 'France' === $zone_name ) {
if ( $initial_cost < 7 ) {
$new_cost = 7;
} elseif ( $initial_cost > 15 ) {
$new_cost = 15;
}
}
// For "Europe" shipping zone name (set min and max cost)
elseif ( 'Europe' === $zone_name ) {
if ( $initial_cost < 10 ) {
$new_cost = 10;
} elseif ( $initial_cost > 25 ) {
$new_cost = 25;
}
}
if ( $new_cost != $initial_cost ) {
$rates[$rate_key]->cost = $new_cost; // Set the new cost
$taxes = []; // Initializing
// Taxes: Loop through the shipping taxes array (as they can be many)
foreach ($rate->taxes as $key => $tax){
if( $tax > 0 ){
$initial_tax_cost = $tax; // Get the initial tax cost
$tax_rate = $initial_tax_cost / $initial_cost; // Get the tax rate conversion
$taxes[$key] = $new_cost * $tax_rate; // Set the new tax cost in taxes costs array
$has_taxes = true; // Enabling tax
}
}
if( isset($has_taxes) && $has_taxes ) {
$rates[$rate_key]->taxes = $taxes; // Set taxes costs array
}
}
}
}
return $rates;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
Don't forget to empty your cart to refresh shipping method caches.