根据 WooCommerce 中客户的总订单更改特定国家/地区的运费
Change shipping cost for specific countries based on total orders by customer in WooCommerce
我遵循这个完美的线程:
.
但就我而言,我需要将折扣应用于运费而非订单总额,我只想针对特定国家/地区(特别是英国)执行此操作。
例如:
- 仅限第一笔订单:6 欧元(统一运费)
- 从第二个订单开始,始终为 25 欧元(统一运费)
我已经将英国的统一费率设置为 6 欧元,但我需要了解如何仅从第二个订单开始收取 25 欧元的附加费。
这是我的代码尝试:
add_filter( 'woocommerce_package_rates', 'free_first_order_shipping', 20, 2 );
function free_first_order_shipping( $rates, $package ) {
if(is_user_logged_in()) {
$user_id = get_current_user_id();
//We want to know how many completed orders customer have or remove status if you dont care.
$args = array(
'customer_id' => 1,
'status' => array('wc-completed'),
);
$orders = wc_get_orders($args);
//If there are no orders returned apply free shipping
if($orders == 0) {
unset( $rates['flat_rate:15'] ); // Shipping 25 euro
}
else{
unset( $rates['betrs_shipping:10-2'] ); // Shipping 6 euro
}
}
return $rates;
}
但是,这并没有调整成本,而是删除了运输方式。有什么建议吗?
关于您的代码的一些注释 attempt/question:
- 你提到的答案是关于增加(负)费用,
woocommerce_package_rates
hook 确实比 woocommerce_cart_calculate_fees
hook 更适合你的问题
- 您可以使用
$package['destination']['country']
来确定国家
unset( $rates['flat_rate:15'] )
不会调整成本但会移除方法
- 要获取客户的订单总数,您可以使用
wc_get_customer_order_count()
函数
所以你得到:
function filter_woocommerce_package_rates( $rates, $package ) {
// ONLY for specific countries
$specific_countries = array( 'UK', 'BE' );
// Checks if a value (country) exists in an array, if not return
if ( ! in_array( $package['destination']['country'], $specific_countries ) ) return $rates;
// Only for logged in users
if ( is_user_logged_in() ) {
// Get user ID
$user_id = get_current_user_id();
// Get the total orders by a customer.
$count = wc_get_customer_order_count( $user_id );
// Loop through
foreach ( $rates as $rate_key => $rate ) {
// Initialize
$has_taxes = false;
// Targeting "Flat Rate" shipping method
if ( $rate->method_id == 'flat_rate' ) {
// Get the initial cost
$initial_cost = $new_cost = $rates[$rate_key]->cost;
// Based on order count
if ( $count == 0 ) {
// Set the new rate cost
$new_cost = 6;
} else {
// Set the new rate cost
$new_cost = 25;
}
// Set the new cost
$rates[$rate_key]->cost = $new_cost;
// Taxes rate cost (if enabled)
$taxes = [];
// Loop through the shipping taxes array (as they can be many)
foreach ($rates[$rate_key]->taxes as $key => $tax ) {
if ( $rates[$rate_key]->taxes[$key] > 0 ) {
// Get the initial tax cost
$initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];
// Get the tax rate conversion
$tax_rate = $initial_tax_cost / $initial_cost;
// Set the new tax cost
$taxes[$key] = $new_cost * $tax_rate;
// Enabling tax
$has_taxes = true;
}
}
// When true
if ( $has_taxes ) {
$rates[$rate_key]->taxes = $taxes;
}
}
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates','filter_woocommerce_package_rates', 10, 2 );
别忘了清空购物车以刷新运输缓存数据!
我遵循这个完美的线程:
但就我而言,我需要将折扣应用于运费而非订单总额,我只想针对特定国家/地区(特别是英国)执行此操作。
例如:
- 仅限第一笔订单:6 欧元(统一运费)
- 从第二个订单开始,始终为 25 欧元(统一运费)
我已经将英国的统一费率设置为 6 欧元,但我需要了解如何仅从第二个订单开始收取 25 欧元的附加费。
这是我的代码尝试:
add_filter( 'woocommerce_package_rates', 'free_first_order_shipping', 20, 2 );
function free_first_order_shipping( $rates, $package ) {
if(is_user_logged_in()) {
$user_id = get_current_user_id();
//We want to know how many completed orders customer have or remove status if you dont care.
$args = array(
'customer_id' => 1,
'status' => array('wc-completed'),
);
$orders = wc_get_orders($args);
//If there are no orders returned apply free shipping
if($orders == 0) {
unset( $rates['flat_rate:15'] ); // Shipping 25 euro
}
else{
unset( $rates['betrs_shipping:10-2'] ); // Shipping 6 euro
}
}
return $rates;
}
但是,这并没有调整成本,而是删除了运输方式。有什么建议吗?
关于您的代码的一些注释 attempt/question:
- 你提到的答案是关于增加(负)费用,
woocommerce_package_rates
hook 确实比woocommerce_cart_calculate_fees
hook 更适合你的问题 - 您可以使用
$package['destination']['country']
来确定国家 unset( $rates['flat_rate:15'] )
不会调整成本但会移除方法- 要获取客户的订单总数,您可以使用
wc_get_customer_order_count()
函数
所以你得到:
function filter_woocommerce_package_rates( $rates, $package ) {
// ONLY for specific countries
$specific_countries = array( 'UK', 'BE' );
// Checks if a value (country) exists in an array, if not return
if ( ! in_array( $package['destination']['country'], $specific_countries ) ) return $rates;
// Only for logged in users
if ( is_user_logged_in() ) {
// Get user ID
$user_id = get_current_user_id();
// Get the total orders by a customer.
$count = wc_get_customer_order_count( $user_id );
// Loop through
foreach ( $rates as $rate_key => $rate ) {
// Initialize
$has_taxes = false;
// Targeting "Flat Rate" shipping method
if ( $rate->method_id == 'flat_rate' ) {
// Get the initial cost
$initial_cost = $new_cost = $rates[$rate_key]->cost;
// Based on order count
if ( $count == 0 ) {
// Set the new rate cost
$new_cost = 6;
} else {
// Set the new rate cost
$new_cost = 25;
}
// Set the new cost
$rates[$rate_key]->cost = $new_cost;
// Taxes rate cost (if enabled)
$taxes = [];
// Loop through the shipping taxes array (as they can be many)
foreach ($rates[$rate_key]->taxes as $key => $tax ) {
if ( $rates[$rate_key]->taxes[$key] > 0 ) {
// Get the initial tax cost
$initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];
// Get the tax rate conversion
$tax_rate = $initial_tax_cost / $initial_cost;
// Set the new tax cost
$taxes[$key] = $new_cost * $tax_rate;
// Enabling tax
$has_taxes = true;
}
}
// When true
if ( $has_taxes ) {
$rates[$rate_key]->taxes = $taxes;
}
}
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates','filter_woocommerce_package_rates', 10, 2 );
别忘了清空购物车以刷新运输缓存数据!