If you run an online store with WooCommerce and want to offer conditional free shipping based on specific product shipping classes, this tutorial is for you. You’ll learn how to:
- Set shipping charges to zero if any product in the cart belongs to a “free shipping” class
- Display user-friendly, styled shipping labels in Bengali showing either the delivery charge or “ডেলিভারি ফ্রী” (free delivery)
- Keep the shipping method names dynamic and localized
Why Customize WooCommerce Shipping Labels?
By default, WooCommerce shows shipping methods with their titles and prices, e.g.,
ঢাকার বাহির: ৳150.00
But what if you want the label to say:
- ঢাকার বাহির: ডেলিভারি ফ্রী (if free shipping applies)
- ঢাকার বাহির: ডেলিভারি চার্জ ৳150.00 টাকা (otherwise)
This improves user experience by making shipping costs clearer and adds localized styling for your Bengali-speaking customers.
How Does This Work? There are two key parts:
- Setting the shipping cost to zero if the cart contains any product with a “free shipping” class
- Changing the shipping label display dynamically based on the cost
The Complete Code
Add the following PHP code snippet to your child theme’s functions.php file or a custom plugin.
/** * 1. Set flat rate shipping cost to 0 if any item has "free-shipping" class */ add_filter( 'woocommerce_package_rates', 'conditionally_free_shipping_for_class', 20, 2 ); function conditionally_free_shipping_for_class( $rates, $package ) { $free_class = 'free-shipping'; // Your free shipping class slug $has_free_shipping_product = false; foreach ( $package['contents'] as $item ) { $product = $item['data']; if ( $product->get_shipping_class() === $free_class ) { $has_free_shipping_product = true; break; } } if ( $has_free_shipping_product ) { foreach ( $rates as $rate_id => $rate ) { if ( 'flat_rate' === $rate->method_id ) { $rates[$rate_id]->cost = 0; if ( ! empty( $rates[$rate_id]->taxes ) ) { foreach ( $rates[$rate_id]->taxes as $key => $tax ) { $rates[$rate_id]->taxes[$key] = 0; } } } } } return $rates; } /** * 2. Show shipping label with Bangla styled text and dynamic charge/title */ add_filter( 'woocommerce_cart_shipping_method_full_label', 'custom_shipping_label_with_styles', 10, 2 ); function custom_shipping_label_with_styles( $label, $method ) { if ( $method->method_id !== 'flat_rate' ) { return $label; } $method_title = $method->get_label(); // e.g. "ঢাকার বাহির" or "ঢাকার ভিতরে" $cost = method_exists( $method, 'get_cost' ) ? $method->get_cost() : $method->cost; if ( $cost == 0 ) { $label = sprintf( '<span style="color: #60ba46; font-weight: bold;">%s: ডেলিভারি ফ্রী</span>', esc_html( $method_title ) ); } else { $label = sprintf( '<span style="color: red; font-weight: bold;">%s: ডেলিভারি চার্জ ৳%s টাকা</span>', esc_html( $method_title ), number_format( $cost, 2 ) ); } return $label; }
Step-by-Step Explanation
1. Checking Products for Free Shipping Class
The first filter woocommerce_package_rates checks all cart items for the product shipping class slug free-shipping (you can change this slug based on your shipping classes).
If any product has this class, the code sets all flat rate shipping costs in the current shipping package to zero, effectively making shipping free.
2. Adjusting the Shipping Label
The second filter woocommerce_cart_shipping_method_full_label modifies how the shipping method label appears on the cart and checkout pages.
It grabs the shipping method’s label (e.g., “ঢাকার বাহির”).
If the shipping cost is zero (free), it shows: ঢাকার বাহির: ডেলিভারি ফ্রী ( with a green colored, bold font )
Otherwise, it shows: ঢাকার বাহির: ডেলিভারি চার্জ ৳150.00 টাকা ( in red colored, bold font )
How to Use?
- Create a shipping class named “Free Shipping” (or your preferred name) in WooCommerce:
- WooCommerce > Settings > Shipping > Shipping classes
- Assign this shipping class to the products that should have free shipping
- Ensure you have Flat Rate shipping enabled in your Shipping Zones
- Add the above PHP code to your theme’s functions.php or a custom plugin
- Test by adding products with and without the free shipping class
Benefits of This Approach
- Dynamically changes shipping cost and label without manual edits
- Localized Bengali labels with user-friendly styling
- Supports multiple flat rate shipping methods with different titles like “ঢাকার বাহির” or “ঢাকার ভিতরে”
- Easy to customize for other languages or styles









