Skip to content

Instantly share code, notes, and snippets.

@BurlesonBrad
Created September 27, 2015 23:35
Show Gist options
  • Save BurlesonBrad/daad96abaa0647ee9e99 to your computer and use it in GitHub Desktop.
Save BurlesonBrad/daad96abaa0647ee9e99 to your computer and use it in GitHub Desktop.
GOAL = Remove flat rate when quantity is OVER 12
add_filter('woocommerce_available_shipping_methods','remove_flat_over_twelve',10,1);
function remove_flat_over_twelve( $available_methods ) {
global $woocommerce;
$minimum_flat_rate_quantity = 12;
if ( $woocommerce->cart->get_cart_contents_count() >= $minimum_flat_rate_quantity ) {
// remove flat_rate shipping
unset($available_methods['flat_rate']);
}
return $available_methods;
}
@BurlesonBrad
Copy link
Author

add_filter('woocommerce_package_rates','remove_flat_over_twelve',10,1);
function remove_flat_over_twelve( $available_methods ) {
    global $woocommerce;
    $minimum_flat_rate_quantity = 13;
    if ( $woocommerce->cart->get_cart_contents_count() >= $minimum_flat_rate_quantity ) {
        // remove flat_rate shipping
        unset($available_methods['flat_rate']);
    }
    return $available_methods;
}

@BurlesonBrad
Copy link
Author

/* from the other gist that seems to be so popular */
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
function hide_shipping_when_free_is_available( $rates, $package ) {
    if ( isset( $rates['free_shipping'] ) ) {
        something about the quantity, then ---> unset( $rates['flat_rate'] );


    return $rates;
}

@vinnyusestrict
Copy link

Run this and see if it's actually running when you expect it. Also check the keys. Remember that WooCommerce caches the requests by zip code, so change the codes when you're testing.

add_filter('woocommerce_package_rates','remove_flat_over_twelve',10,1);

function remove_flat_over_twelve( $rates ) 
{
    global $woocommerce;
error_log(__LINE__ . ' In woocommerce_package_rates filter');
    $minimum_flat_rate_quantity = 12;
    $num_contents = $woocommerce->cart->get_cart_contents_count(); 
error_log( __LINE__ . ' Cart num contents: ' . print_r($num_contents,1));   
    if ( $num_contents > $minimum_flat_rate_quantity ) 
    {
error_log(__LINE__ . ' rates keys: ' . print_r(array_keys($rates),1));      
        // remove flat_rate shipping
        unset($rates['flat_rate']);
    }
error_log(__LINE__ . ' Returning keys: ' . print_r(array_keys($rates),1));  
    return $rates;
}

@patchgill
Copy link

Hi Brad I quickly ran the function on a mock instance to test and following works fine here http://test.pscreativeagency.co.uk/shop/. Once the basket has 13+ items the flat fee is removed. You do have to clear the WC Transients though as it did cache on me.

add_filter('woocommerce_package_rates','remove_flat_over_twelve',10,1);

function remove_flat_over_twelve( $rates ) 
{
    global $woocommerce;
    $minimum_flat_rate_quantity = 12;
    $num_contents = $woocommerce->cart->get_cart_contents_count();  
    if ( $num_contents > $minimum_flat_rate_quantity ) 
    {      
        // remove flat_rate shipping
        unset($rates['flat_rate']);
    }

    return $rates;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment