• Resolved TC.K

    (@wp_dummy)


    I want to hide other shipping method when Free Shipping is available. I don’t know if this is included by default.
    But when I examine the source codes, I do find codes that make this happened. But unfortunately it is not working on my site.

    I did try with following codes:

    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 );

    But since the filter is not fired. Probably the filter is overrides?

Viewing 1 replies (of 1 total)
  • Hi,
    kindly add below code

    //Please add this code in the function.php of the current active theme :
    function hide_shipping_when_free_is_available( $rates ) {
        $free = array();
        foreach ( $rates as $rate_id => $rate ) {
                if ( 'Free shipping' === $rate->label ) {
                        $free[ $rate_id ] = $rate;
                        break;
                }
        }
        return ! empty( $free ) ? $free : $rates;
    }
    add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 100 );

    clear your cart then please Clear your WooCommerce cache by navigating to WooCommerce > Status > Tools > WC Transients > Clear transients.

    check your site.

    Regards,

Viewing 1 replies (of 1 total)
  • The topic ‘Hide other shipping method when Free Shipping is available’ is closed to new replies.