Hide specifics Flat Rates when Free Shipping is available in WooCommerce 3

前端 未结 1 1088
我寻月下人不归
我寻月下人不归 2021-01-23 03:46

In WooCommerce 3, I have these shipping options (settings):

  1. Free Shipping: free_shipping:1 - Minimum order amount is set at $50.
相关标签:
1条回答
  • 2021-01-23 04:22

    Based on the official WooCommerce snippet code, making some light changes, you will be able to hide only your first flat rate when free shippings is available:

    add_filter( 'woocommerce_package_rates', 'conditionally_hide_shipping_methods', 100, 2 );
    function conditionally_hide_shipping_methods( $rates, $package ) {
        // HERE yours 2nd flat rate "Express Shipping" (that you never hide) in the array:
        $flat_rates_express = array( 'flat_rate:5', 'flat_rate:12', 'flat_rate:14' );
    
        $free = $flat2 = array();
        foreach ( $rates as $rate_key => $rate ) {
            // Updated Here To 
            if ( in_array( $rate->id, $flat_rates_express ) )
                $flat2[ $rate_key ] = $rate;
            if ( 'free_shipping' === $rate->method_id )
                $free[ $rate_key ] = $rate;
        }
        return ! empty( $free ) ? array_merge( $free, $flat2 ) : $rates;
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Tested on WooCommerce 3 and works.

    Refresh the shipping caches:
    1) First empty your cart.
    2) This code is already saved on your function.php file.
    3) Go in a shipping zone settings and disable one "flat rate" (for example) and "save". Then re-enable that "flat rate" and "save". You are done and you can test it.

    0 讨论(0)
提交回复
热议问题