The Solution
The code snippet provided below will add a progressive cart fee based on the customer's shipping country and the product categories in their cart. Here's how it works:
-
Valid Countries: The code starts by defining an array of valid countries, including 'IT' (Italy). This allows you to target specific regions or countries where you want to apply the additional fees.
-
Product Category Groups: Next, the code defines three product category groups: $product_cat1
, $product_cat2
, and $product_cat3
. Each group is associated with a different fee amount, allowing you to differentiate the pricing based on the product category.
-
Fee Calculation: The code then iterates through the customer's cart items and checks which product category groups are present. If a product from $product_cat1
is in the cart and the customer's shipping country is 'IT', a fee of 10 is applied. If a product from $product_cat2
or $product_cat3
is in the cart and the customer's shipping country is not 'IT', a fee of 30 or 50 is applied, respectively.
-
Fee Application: Finally, the code adds the calculated fee to the customer's cart using the add_fee()
function provided by Woocommerce.
By implementing this code in your Woocommerce site's functions.php
file, you can easily manage the additional fees based on the customer's location and the product categories in their cart. This can help you cover the increased costs associated with specific products or shipping destinations, ultimately improving your store's profitability.
add_action('woocommerce_cart_calculate_fees', 'conditional_packing_fee', 20, 1 );
function conditional_packing_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
// HERE the valid countries (including 'IT'):
$valid_countries = array('BE','EL','LT','PT','BG','ES','LU','RO','CZ','FR','HU','SI','DK','HR',
'MT','SK','DE','NL','FI','EE','CY','AT','SE','IE','LV','PL','UK','IT');
$user_country = WC()->customer->get_shipping_country(); // Get the user shipping country
if( ! in_array($user_country, $valid_countries) ) return; // Not a valid country, we EXIT
// HERE your product categories groups:
$product_cat1 = array(1,2,3,4,5,6); // Group 1 ==> cost 10
$product_cat2 = array(4,5,6); // Group 2 ==> cost 30
$product_cat3 = array(1,2,3); // Group 3 ==> cost 50
$found1 = $found2 = $found3 = false;
$fee = 0;
foreach ( $cart->get_cart() as $cart_item ) {
// Group 1 case
if( has_term( $product_cat1, 'product_cat', $cart_item['product_id'] ) && 'IT' == $user_country ){
$found1 = true;
$break; // We can stop the loop for Italy (if found)
}
// Group 2 case
elseif( has_term( $product_cat2, 'product_cat', $cart_item['product_id'] ) && 'IT' != $user_country ){
$found2 = true;
}
// Group 3 case
elseif( has_term( $product_cat3, 'product_cat', $cart_item['product_id'] ) && 'IT' != $user_country ){
$found3 = true;
}
}
if( $found1 ){
$fee = 10; // Italy
$label = __('Packing fee', 'woocommerce');
} else {
if( $found3 ){
$fee = 50; // Large packs (can have medium packs too)
$label = __('Large packs', 'woocommerce');
} elseif( $found2 && ! $found3 ){
$fee = 30; // Medium packs (only)
$label = __('Large packs', 'woocommerce');
}
}
if ( $fee > 0 )
$cart->add_fee( $label, $fee, true, 'standard' );
}
This code should be placed in the functions.php
file of your active child theme (or active theme). It will work seamlessly with your Woocommerce store, allowing you to implement dynamic pricing and improve your store's profitability.
Remember to customize the $valid_countries
array and the $product_cat1
, $product_cat2
, and $product_cat3
arrays to match your specific requirements. You can add or remove countries and adjust the fee amounts as needed.
By leveraging this solution, you can provide a better shopping experience for your customers while ensuring your store's financial sustainability. If you need further assistance or have any questions, feel free to contact Flowpoint.ai, our team of experts can help you optimize your Woocommerce store and identify technical errors that may be impacting your conversion rates