• Resolved laserjones

    (@laserjones)


    Hello,

    I found that when free shipping is offered (e.g. due to a certain minimum cart subtotal exceeded by the customer), the flat rate shipping option is still offered as well.

    I consider this a bug, as it does not make sense to offer both, and it confuses the customer. I suggest that this should be fixed in a future release.

    In an older post I found a code snippet for hiding all other shipping methods if free shipping is offered, but
    1) that post was several years old, and much has changed in WooCommerce since then (e.g. shipping zones), and
    2) I don’t want *all* shipping methods to be hidden, only flat rate shipping. Local pickup should still be offered.

    If anyone has a solution for me until this bug gets finally fixed, I would greatly appreciate it.

    Best regards,
    J?rg

    https://www.ads-software.com/plugins/woocommerce/

Viewing 9 replies - 16 through 24 (of 24 total)
  • ok. i know. tnx i search.

    but i thinking … my theme is place, install … and setting … is possibile now change thme normal in a child theme ?

    if i past it in a functions.php in a normal theme ?

    ?? tnx

    Thread Starter laserjones

    (@laserjones)

    You could also change the code in your normal functions.php and not use a child theme. But then each update of your theme could overwrite the modified code, so you would have to change it again. A child theme remains unaffected by updates of the main theme, so it’s a better way for implementing customizations.

    Kind of on the same topic
    I can get this code to work only when I refresh the page, and it works as needed:

    
    function my_hide_shipping_when_free_is_available( $rates ) {
        
        $free = array();
    	
        if ( isset( $_POST['post_data'] ) ) {
            parse_str( $_POST['post_data'], $post_data );
        } else {
            $post_data = $_POST; // fallback for final checkout (non-ajax)
        }
    	
    	foreach ( $rates as $rate_id => $rate ) {
    		if ( 'free_shipping' === $rate->method_id && isset($post_data['customorderfield']) && $post_data['customorderfield'] == 1) {
    			$free[ $rate_id ] = $rate;
    			break;
    		}
    	}
    	
        if ( isset( $rates['free_shipping:10'] ) ) {
            unset( $rates['free_shipping:10'] );
        }   
    	
    	return ! empty( $free ) ? $free : $rates;
    }
    add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available' );

    However, what I’m trying to do is have shipping update via ajax.
    So I have added this js to trigger a cart update it does work and send the post field

    
    add_action( 'wp_footer', 'woocommerce_sendthis_js' );
    function woocommerce_sendthis_js() {
        if (is_checkout()) {
        ?>
        <script type="text/javascript"> 
        jQuery( document ).ready(function( $ ) {
    	    
    			    jQuery('#mycheckbox').on( 'change', function () {  
    			       jQuery('body').trigger('update_checkout', { update_shipping_method: true } );
    			    });
        });
        </script>
        <?php
        }
    }
    

    I added this snippet of code to my woocommerce fields:

    <input id="mycheckbox" class="input-checkbox update_totals_on_change" type="checkbox" name="customorderfield" value="1" />

    Again, the shipping updates properly when I refresh the page, but won’t update upon triggering update_checkout, and I’m not sure why. Any ideas?

    • This reply was modified 8 years, 3 months ago by dailce.
    mttindustries

    (@mttindustries-1)

    The issue I have is that I have all three flat rate, free shipping and local pick up. If using the code first suggested it disabled the local pick up option if the item is free shipping. If the item can be shipped free (ie over $250) I still want there to display the local pick up as an option. How do I still allow that? My shipping isn’t restricted to different zones, this is just the stand shipping country wide.

    I also agree I think this is a bug because you need to be making the product woocomerce user friendly for the most simplest of people. Our clientel wouldn’t know any coding and so this needs to be inbuilt in the backed end. Also what happens when woocommerce updates and the code isn’t valid anymore?

    Hey Guys!

    I have no zones, just a fixed price for shipping and free shipping when you shop for a certain amount and local pick up.

    What would i need to change in order to get this code to work for me?

    I want to hide the “Fixed Price Shipping” when the “Free shipping” option is visable, but at the same time offer “Local Pick up”.

    Would appreciate your help!

    Can’t understand why Woocommerce won’t give me the option to hide and show shipping methods as i want!

    Hi all, I wrote the following code to fix it:

    add_filter("woocommerce_package_rates", function ($rates, $package) {
        $methods = array();
        foreach (array_keys($rates) as $rate) { 
            $methods[preg_replace('/^(.+?):.*/', '${1}', $rate)] = $rate;
        }
        if (in_array("free_shipping", array_keys($methods))) {
            unset($rates[$methods["flat_rate"]]);
        }
        return $rates;
    });

    Best regards,
    Miguel.

    Hey

    I have the same problem, i try to paste the code into the functions but it is the same problem. I would like to appear only free shipping if it is available, but it shows also flat rate delivery and pick up.

    Thread Starter laserjones

    (@laserjones)

    dadisk8, you would probably have to add another “unset” line to hide pick-up as well. The above code only hides flat rate shipping (but not pick-up) when free shipping is available. But if not even that works for you, I have no idea what could be wrong.

    Thanks @mfcorral, it works like a charm.

    • This reply was modified 7 years, 7 months ago by reddatos.
Viewing 9 replies - 16 through 24 (of 24 total)
  • The topic ‘Flat rate shipping still offered when free shipping is available’ is closed to new replies.