An easy solution to adjust the displayed shipping costs as”free” when the cart total is greater than or equal to a certain amount by using this handy code snippet given below.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function ts_filter_woocommerce_package_rates( $rates, $package ) {
// Cart total amount (integer)
$cart_total = WC()->cart->cart_contents_total;
// Greater than or equal to
if ( $cart_total >= 59.99 ) {
foreach ( $rates as $rate_key => $rate ) {
// For "free shipping" method, do not remove it
if ( $rate->method_id != 'free_shipping' ) {
// Append rate label titles (free)
$rates[$rate_key]->label .= ' ' . __( '(free)', 'woocommerce' );
// Set rate cost
$rates[$rate_key]->cost = 0;
// Set taxes rate cost (if enabled)
$taxes = array();
foreach ( $rates[$rate_key]->taxes as $key => $tax ) {
if ( $rates[$rate_key]->taxes[$key] > 0 ) {
$taxes[$key] = 0;
}
}
$rates[$rate_key]->taxes = $taxes;
}
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'ts_filter_woocommerce_package_rates', 10, 2 );
function ts_filter_woocommerce_package_rates( $rates, $package ) {
// Cart total amount (integer)
$cart_total = WC()->cart->cart_contents_total;
// Greater than or equal to
if ( $cart_total >= 59.99 ) {
foreach ( $rates as $rate_key => $rate ) {
// For "free shipping" method, do not remove it
if ( $rate->method_id != 'free_shipping' ) {
// Append rate label titles (free)
$rates[$rate_key]->label .= ' ' . __( '(free)', 'woocommerce' );
// Set rate cost
$rates[$rate_key]->cost = 0;
// Set taxes rate cost (if enabled)
$taxes = array();
foreach ( $rates[$rate_key]->taxes as $key => $tax ) {
if ( $rates[$rate_key]->taxes[$key] > 0 ) {
$taxes[$key] = 0;
}
}
$rates[$rate_key]->taxes = $taxes;
}
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'ts_filter_woocommerce_package_rates', 10, 2 );
function ts_filter_woocommerce_package_rates( $rates, $package ) { // Cart total amount (integer) $cart_total = WC()->cart->cart_contents_total; // Greater than or equal to if ( $cart_total >= 59.99 ) { foreach ( $rates as $rate_key => $rate ) { // For "free shipping" method, do not remove it if ( $rate->method_id != 'free_shipping' ) { // Append rate label titles (free) $rates[$rate_key]->label .= ' ' . __( '(free)', 'woocommerce' ); // Set rate cost $rates[$rate_key]->cost = 0; // Set taxes rate cost (if enabled) $taxes = array(); foreach ( $rates[$rate_key]->taxes as $key => $tax ) { if ( $rates[$rate_key]->taxes[$key] > 0 ) { $taxes[$key] = 0; } } $rates[$rate_key]->taxes = $taxes; } } } return $rates; } add_filter( 'woocommerce_package_rates', 'ts_filter_woocommerce_package_rates', 10, 2 );
Output
The output shows that the label of shipping option costs are replaced as ‘free’ when the cart total is greater than or equal to $59.99.
Additionally, you can set all shipping methods cost to zero for a free shipping coupon in WooCommerce where this feature is especially useful in clearance sale offering both free shipping and coupon discounts.