使用下面的代码,我可以使用特定的运费方法(这里是'easypack_parcel_machines')将成本设置为0,当cart小计达到特定金额时(在这里,150兹罗提)
function override_inpost_cost( $rates, $package ) {
// Make sure paczkomaty is available
if ( isset( $rates['easypack_parcel_machines'] ) ) {
// Current value of the shopping cart
$cart_subtotal = WC()->cart->subtotal;
// Check if the subtotal is greater than 150pln
if ( $cart_subtotal >= 150 ) {
// Set the cost to 0pln
$rates['easypack_parcel_machines']->cost = 0;
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'override_inpost_cost', 10, 2 );但问题是,航运税的原始成本仍然存在,即使“easypack_parcel_machines”运输方法费率成本被设置为零其原始成本(因为它是应纳税的)。
如何更改代码,以便如果“easypack_parcel_machines”运输方法费率成本设置为0,则税收也将设置为零?
发布于 2020-11-19 20:58:41
注意: As cart可以通过一些插件或自定义代码分成多个配送包,正确的方法是获取当前配送包中包含的相关购物车项目的小计。
代码中缺少的是将税收设置为零,如下所示:
add_filter( 'woocommerce_package_rates', 'override_inpost_shipping_method_cost', 10, 2 );
function override_inpost_shipping_method_cost( $rates, $package ) {
$targeted_shipping_rate_id = 'easypack_parcel_machines'; // <== Define shipping method rate Id
// Make sure that our shipping rate is available
if ( isset( $rates[$targeted_shipping_rate_id] ) ) {
$cart_subtotal_incl_tax = 0; // Initializing
// Get cart items subtotal for the current shipping package
foreach( $package['contents'] as $cart_item ) {
$cart_subtotal_incl_tax += $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
}
// Check if the subtotal is greater than 150pln
if ( $cart_subtotal_incl_tax >= 150 ) {
// Set the cost to 0pln
$rates[$targeted_shipping_rate_id]->cost = 0;
$taxes = array(); // Initializing
// Loop through the shipping method rate taxes array
foreach( $rates[$targeted_shipping_rate_id]->taxes as $key => $tax_cost ) {
$taxes[$key] = 0; // Set each tax to Zero
}
if ( ! empty($taxes) ) {
$rates[$targeted_shipping_rate_id]->taxes = $taxes; // Set back "zero" taxes array
}
}
}
return $rates;
}代码位于活动子主题(或活动主题)的functions.php文件中。应该行得通。
注意:不要忘记清空您的购物车来刷新已缓存的数据。
https://stackoverflow.com/questions/64919234
复制相似问题