[英]Hide custom shipping fee cost if the value is 0 in Woocommerce
In Woocommerce I have added an additional shipping cost to the cart using the fee API.在 Woocommerce 中,我使用费用 API 向购物车添加了额外的运费。
My code is:我的代码是:
add_action( 'woocommerce_cart_calculate_fees', 'pt_add_handling_fee' );
function pt_add_handling_fee() {
$title = 'Maggiorazione Isole & Calabria';
global $woocommerce;
$peso_totale = $woocommerce->cart->cart_contents_weight;
$provincia = $woocommerce->customer->get_shipping_state();
// echo 'weight'.$woocommerce->cart->cart_contents_weight;
// echo 'price'.$woocommerce->cart->subtotal;
// Maggiorazione 5€ ogni 100kg per la Sardegna
if ($peso_totale > 99 && ($provincia == "CA" or $provincia == "NU")) {
$fee = $peso_totale / 100 * 5;
}
// Maggiorazione 3€ ogni 100kg per la Sicilia
if ($peso_totale > 99 && ($provincia == "AG" or $provincia == "CL")) {
$fee = $peso_totale / 100 * 3;
}
// Maggiorazione 2€ ogni 100kg per la Calabria
if ($peso_totale > 99 && ($provincia == "CZ" or $provincia == "CS")) {
$fee = $peso_totale / 100 * 2;
}
$woocommerce->cart->add_fee( $title, $fee, TRUE, 'standard' );
}
So I apply a surcharge for some provinces, if the total weight is greater than 99 kg.所以我对一些省份申请附加费,如果总重量大于99公斤。
But the problem is that for other provinces, the fee is still applied with a value of "0 €".但问题是,对于其他省份,该费用仍然适用于“0€”的值。
How to make this fee applied only for the defined provinces, when the total cart weight is over 99 Kg?当总重量超过 99 Kg 时,如何使此费用仅适用于定义的省份?
It's possible?这是可能的? Any help is appreciated.任何帮助表示赞赏。
Your code is a bit outdated and woocommerce_cart_calculate_fees
action hook has the WC_Cart
object as available argument.您的代码有点过时, woocommerce_cart_calculate_fees
操作挂钩将WC_Cart
对象作为可用参数。
global $woocommerce
with $woocommerce->cart
or $woocommerce->customer
are now simply replaced by:带有$woocommerce->cart
或$woocommerce->customer
$woocommerce->cart
global $woocommerce
现在简单地替换为:
WC()->cart
(the instance of the WC_Cart
object) WC()->cart
( WC_Cart
对象的实例)WC()->customer
(the instance of the WC_Customer
object) WC()->customer
( WC_Customer
对象的实例)To make the fee applied only for your specific conditions, try this revisited code:要使费用仅适用于您的特定条件,请尝试使用以下重新访问的代码:
add_action( 'woocommerce_cart_calculate_fees', 'conditional_shipping_surcharge', 10, 1 );
function conditional_shipping_surcharge( $cart ) {
$targeted_weight = 100; // Define the starting targeted weight
$cart_weight = $cart->get_cart_contents_weight();
$shipping_state = WC()->customer->get_shipping_state();
$percent = 0; // Initializing
if ( $cart_weight >= $targeted_weight ) {
// 5 € of surcharge per 100kg for Sardinia
if ( in_array( $shipping_state, array('CA', 'NU') ) ) {
$percent = 5;
}
// 3 € of surcharge per 100kg for Sicily
elseif ( in_array( $shipping_state, array('AG', 'CL') ) ) {
$percent = 3;
}
// 2 € of surcharge per 100kg for Calabria
elseif ( in_array( $shipping_state, array('CZ', 'CS') ) ) {
$percent = 2;
}
}
if( $percent > 0 ){
// Fee calculation
$fee = $cart_weight * $percent / 100;
// Add calculated fee (Taxable for "standart" default tax class)
$cart->add_fee( __("Maggiorazione Isole & Calabria", "woocommerce"), $fee, true );
}
}
Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.