woocommerce show/hide flat rate based on product quantity

After long search for a good plugin couldn’t find anything so i created this snippets to help me in this subject

//Show/Hide FLATE RATE Shipping Based on Item Count for Domestic Shipment

add_filter('woocommerce_package_rates', 'show_hide_flat_rate_shipping', 10, 2);
function show_hide_flat_rate_shipping($available_shipping_methods, $package){
    $minimum_number_of_item = 10; //Give here minumum number of items to allow freeshipping
    

    $flat_rate  =   true;
    global $woocommerce;
    

    $item_count = 0;
    foreach (WC()->cart->cart_contents as $key => $item) {
        $item_count += $item['quantity'];
    }

    if( $item_count >= $minimum_number_of_item ){ //if products is greater or equal to the minumum number in my case 10 
        $flat_rate  =   false;
    }

    if($flat_rate){ //if flat_rate is true
     //do nothing show all shipping methods
    }
    else{ //if flat_rate is false
        foreach($available_shipping_methods as $shipping_method =>  $method){
            if( strpos( $shipping_method, 'flat_rate' ) !== false ) {
                unset($available_shipping_methods[$shipping_method]);
            }
        }
    }
	
    return $available_shipping_methods;
	
}
Advertisements

Leave a Reply :)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s