In Woocommerce I have added an additional shipping cost to the cart using the fee 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.
But the problem is that for other provinces, the fee is still applied with a value of "0 €".
How to make this fee applied only for the defined provinces, when the total cart weight is over 99 Kg?
It's possible? Any help is appreciated.