Ecommerce Shopify WordPress Discussion

WooCommerce: Hide other shipping methods except local pickup when free shipping is available

I do not want to hide Local Pickup when free shipping is available. Removing local pickup makes no sense, but I cannot figure out how to not remove it using the official code. /** * Hide shipping rates when free shipping is available. * Updated to support WooCommerce 2.6 Shipping Zones. * * @param array $rates Array of rates found for the package. * @return array */ function my_hide_shipping_when_free_is_available( $rates ) { $free = array(); foreach ( $rates as $rate_id => $rate ) { if ( 'free_shipping' === $rate->method_id ) { $free[ $rate_id ] = $rate; break; } } return ! empty( $free ) ? $free : $rates; } add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 ); This is my attempt in removing flat_rate1 since that is, for me, the paid option. Again, I want to keep FREE shipping and LOCAL pickup. add_filter( 'woocommerce_package_rates', 'hide_shipping_except_local_when_free_is_available', 100 ); function hide_shipping_except_local_when_free_is_available($rates) { $free = array(); foreach ($rates as $rate_id => $rate) { if ('free_shipping' === $rate->method_id) { foreach($rates as $rate_id => $rate) { if ('flat_rate1' === $rate->method_id ) unset($rates[ $rate_id ]); } break; } } return !empty( $free ) ? $free : $rates; }
To hide all shipping methods except local pickup and free shipping methods when free shipping is available, use the following: add_filter( 'woocommerce_package_rates', 'hide_shipping_except_local_when_free_is_available', 100 ); function hide_shipping_except_local_when_free_is_available($rates) { $free = $local = array(); foreach ( $rates as $rate_id => $rate ) { if ( 'free_shipping' === $rate->method_id ) { $free[ $rate_id ] = $rate; } elseif ( 'local_pickup' === $rate->method_id ) { $local[ $rate_id ] = $rate; } } return ! empty( $free ) ? array_merge( $free, $local ) : $rates; } Code goes in functions.php file of the active child theme (or active theme). Tested and works. Related: How to hide free shipping when there is shipping costs in WooCommerce Hide specifics Flat Rates when Free Shipping is available in WooCommerce 3 WooCommerce - Hide other shipping methods when FREE SHIPPING is available

February 4, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.