WooCommerce 自定义运费最低要求小计含税计算
WooCommerce custom shipping costs for min required subtotal with tax calculations
我在 WooCommerce 中设置了一些启用了应税选项的运输方式。如果花费超过 X 金额,我将使用此代码设置不同的运费:
// Extra discount on shipping for orders of values of above 150 or 100.
function custom_adjust_shipping_rate( $rates ) {
$cart_subtotal = WC()->cart->subtotal;
// Check if the subtotal is greater than value specified
if ( $cart_subtotal >= 29.99 ) {
// Loop through each shipping rate
foreach ( $rates as $rate ) {
// Store the previous cost in $cost
$cost = $rate->cost;
// Adjust the cost as needed
// original shipping greater than 6 discount by 5
if ( $cost == 7.38 ) {
// discount rate by 5
$rate->cost = $cost - 2.54;
}
// Optional discount for other shipping rates
if ( $cost == 4.10 ) {
$rate->cost = $cost - 5;
}
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'custom_adjust_shipping_rate', 10 );
但问题是税收计算。我的代码不处理税收。
如何调整自定义运费的税费?
如何在成本计算中启用税收?
如有任何建议,我们将不胜感激。
要在您的代码中启用运费税计算,请改用以下重新访问的代码:
add_filter( 'woocommerce_package_rates', 'adjust_shipping_rates_cost', 10, 2 );
function adjust_shipping_rates_cost( $rates, $package ) {
$min_subtotal = 30; // Set min subtotal
$cart_subtotal = 0; // Initializing
// Loop through cart items to get items total for the current shipping package
foreach( $package['contents'] as $item ) {
$cart_subtotal += $item['line_subtotal'] + $item['line_subtotal_tax'];
// $cart_subtotal += $item['line_subtotal']; // Or without taxes
}
// Check if the subtotal is greater than specified value
if ( $cart_subtotal < $min_subtotal ) {
return $rates; // Exit
}
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ) {
$has_taxes = false; // Initializing
$taxes = array(); // Initializing
$new_cost = $initial_cost = $rate->cost; // grab initial cost
if ( $initial_cost == 7.38 ) {
$new_cost -= 2.54;
} elseif ( $initial_cost == 4.10 ) {
$new_cost -= 5;
}
$rates[$rate_key]->cost = $new_cost; // Set new rate cost
// Loop through taxes array (change taxes rate cost if enabled)
foreach ($rate->taxes as $key => $tax){
if( $tax > 0 ){
// Get the tax rate conversion
$tax_rate = $tax / $initial_cost;
// Set the new tax cost in the array
$taxes[$key] = $new_cost * $tax_rate;
$has_taxes = true; // Enabling tax changes
}
}
// set array of shipping tax cost
if( $has_taxes ) {
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。应该可以。
不要忘记清空购物车以刷新运输缓存数据。
我在 WooCommerce 中设置了一些启用了应税选项的运输方式。如果花费超过 X 金额,我将使用此代码设置不同的运费:
// Extra discount on shipping for orders of values of above 150 or 100.
function custom_adjust_shipping_rate( $rates ) {
$cart_subtotal = WC()->cart->subtotal;
// Check if the subtotal is greater than value specified
if ( $cart_subtotal >= 29.99 ) {
// Loop through each shipping rate
foreach ( $rates as $rate ) {
// Store the previous cost in $cost
$cost = $rate->cost;
// Adjust the cost as needed
// original shipping greater than 6 discount by 5
if ( $cost == 7.38 ) {
// discount rate by 5
$rate->cost = $cost - 2.54;
}
// Optional discount for other shipping rates
if ( $cost == 4.10 ) {
$rate->cost = $cost - 5;
}
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'custom_adjust_shipping_rate', 10 );
但问题是税收计算。我的代码不处理税收。
如何调整自定义运费的税费?
如何在成本计算中启用税收?
如有任何建议,我们将不胜感激。
要在您的代码中启用运费税计算,请改用以下重新访问的代码:
add_filter( 'woocommerce_package_rates', 'adjust_shipping_rates_cost', 10, 2 );
function adjust_shipping_rates_cost( $rates, $package ) {
$min_subtotal = 30; // Set min subtotal
$cart_subtotal = 0; // Initializing
// Loop through cart items to get items total for the current shipping package
foreach( $package['contents'] as $item ) {
$cart_subtotal += $item['line_subtotal'] + $item['line_subtotal_tax'];
// $cart_subtotal += $item['line_subtotal']; // Or without taxes
}
// Check if the subtotal is greater than specified value
if ( $cart_subtotal < $min_subtotal ) {
return $rates; // Exit
}
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ) {
$has_taxes = false; // Initializing
$taxes = array(); // Initializing
$new_cost = $initial_cost = $rate->cost; // grab initial cost
if ( $initial_cost == 7.38 ) {
$new_cost -= 2.54;
} elseif ( $initial_cost == 4.10 ) {
$new_cost -= 5;
}
$rates[$rate_key]->cost = $new_cost; // Set new rate cost
// Loop through taxes array (change taxes rate cost if enabled)
foreach ($rate->taxes as $key => $tax){
if( $tax > 0 ){
// Get the tax rate conversion
$tax_rate = $tax / $initial_cost;
// Set the new tax cost in the array
$taxes[$key] = $new_cost * $tax_rate;
$has_taxes = true; // Enabling tax changes
}
}
// set array of shipping tax cost
if( $has_taxes ) {
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。应该可以。
不要忘记清空购物车以刷新运输缓存数据。