How to Hide Shipping Methods Based On Destination Countries (US & Canada)? – Code Snippet

This article explains how to offer free shipping only for the United States and Canada billing addresses and, make available the international USPS shipping rates for all other countries. 

This code snippet has created currently for ELEX Stamps.com Shipping Plugin with USPS Postage for WooCommerce with Print Label, but it is applicable to other ELEX shipping plugins as well by adding the shipping class list (wf_usps_stamps)

Also, you can hide shipping methods for more countries by adding ($country_list ).

Hide Shipping Methods Based On Destination Countries | free-shipping

 

Add the below code snippet in the Theme Functions (functions.php) file of your activated website theme. 

Just copy and paste the snippet in Appearance >Theme Editor > functions.php. You are done!

Code Snippet to Hide Shipping Method Based on the Destination Countries (US, Canada) 

add_filter('woocommerce_package_rates', 'wf_remove_shipping_options_for_particular_country', 10, 2);    
function wf_remove_shipping_options_for_particular_country($available_shipping_methods, $package){
    global $woocommerce;  
    $country_list = array(
        'US',
        'CA',
    ); 
    $shipping_class_list = array(
        'wf_usps_stamps',
    );
    $customer_country = $woocommerce->customer->get_shipping_country();
    $available_shipping_methods_temp = $available_shipping_methods;
    foreach($available_shipping_methods_temp as $key=>$methods){
        $shipping_class = explode(":",$key);
        if(in_array($shipping_class[0] ,$shipping_class_list) && in_array($customer_country,$country_list)){
            unset($available_shipping_methods[$key]);
        }
    }
    return $available_shipping_methods; 
}

Cart Page After Adding the Snippet 

After adding the code snippet, the cart page will look like as in the below screenshot:

If you are looking for a plugin to do this job, visit ELEX Hide Shipping Methods Plugin. Using this plugin, You can easily hide shipping methods based on various conditions.


To know more about the product, check out Stamps.com Shipping Plugin with USPS Postage for WooCommerce.

Or check out the documentation section for more related articles.

For any queries, contact our support team.

 

Next Hide Checkout Fields Based on the Shipping Method: WooCommerce (With Video)

    Leave a Reply

    WordPress Image Lightbox Plugin