Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Disable Shipping for Certain Products and Countries in WooCommerce?

When shipping methods are disabled due to product restrictions, a personalized message can be displayed with the code snippet given below. This message informs customers that specific items, identified by their names, cannot be delivered to the chosen country.

// HERE your settings - Utility function
function ts_your_country_shipping_settings(){
    $results = array();
    // Can be based on "Product IDs" (or "Product categories" ==> false)
    $results['type'] = true; // or false

    // Allowed countries (Only compatible country codes - 2 digits)
    $results['countries'] = array(  'US', 'IN' );

    if( $results['type'] ){
        // Restricted product IDs
        $results['matching'] = array( 100, 470 );
    } else {
        // Restricted product categories (IDs, Slugs or Names)
        $results['matching'] = array('accessories', 'electronic' );
    }
    // Message
    $results['message'] = __( "can not be delivered to your country.", "woocommerce" );

    return $results;
}

// Utility function that check cart items
function ts_get_items_names( $matching, $package, $type ){
    $product_names = array();

    // Search in cart items
    foreach( $package['contents'] as $item ){
        if( $type ){
            if( in_array( $item['data']->get_id(), $matching ) )
                $product_names[] = $item['data']->get_name();
        } else {
            if( has_term( $matching, 'product_cat', $item['product_id'] ) )
                $product_names[] = $item['data']->get_name();
        }
    }
    return $product_names;
}

// Conditionally disabling shipping methods
add_filter('woocommerce_package_rates','custom_country_shipping_rules', 10, 2 );
function custom_country_shipping_rules( $rates, $package ){
    if( isset($package['destination']['country']) && isset($package['contents']) ){
        // Load your settings
        $data = ts_your_country_shipping_settings();

        // If matching allowed countries ==> We Exit
        if( in_array( $package['destination']['country'], $data['countries'] ) )
            return $rates; // Exit

        $product_names = ts_get_items_names( $data['matching'], $package, $data['type'] );

        // When product match we Remove all shipping methods
        if( count($product_names) > 0 ){
            // Removing all shipping methods
            foreach( $rates as $rate_id => $rate )
                unset( $rates[$rate_id] );
        }
    }
    return $rates;
}

// Conditionally displaying a shipping message
add_filter('woocommerce_cart_no_shipping_available_html','ts_custom_country_shipping_notice', 10, 1 );
add_filter('woocommerce_no_shipping_available_html','ts_custom_country_shipping_notice', 10, 1 );
function ts_custom_country_shipping_notice( $html ){
    $package = WC()->shipping->get_packages()[0];
    if( isset($package['destination']['country']) && isset($package['contents']) ){
        // Load your settings
        $data = ts_your_country_shipping_settings();

        // If matching allowed countries ==> We Exit
        if( in_array( $package['destination']['country'], $data['countries'] ) )
            return $html; // Exit

        $product_names = ts_get_items_names( $data['matching'], $package, $data['type'] );

        if( count($product_names) > 0 ){
            $text = '"' . implode( '", "', $product_names ) . '" ' . $data['message'];
            $html  = wpautop( $text );
        }
    }
    return $html;
}

Output

When a customer selects products from the electronic category and updates the address to the country specified in the code, a shipping message is displayed as shown below.

How to Disable Shipping for Certain Products and Countries in WooCommerce? - Tyche Softwares

Read Related Article: How to Display Only ‘Local Pickup’ if Available in WooCommerce?

Instead of countries, you can also choose to hide a shipping method based on product and state in WooCommerce which will help you to provide customers with relevant and accurate shipping options.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x