2

I am trying to disable shipping for a specific product if customer shipping country is not Italy

Here is my code, but I don't know how to set the country condition:

function hide_shipping_when_class_is_in_cart( $rates, $package ) {
    // shipping class IDs that need the method removed
    $shipping_classes = array('bulky-items');
    $if_exists = false;

    foreach( $package['contents'] as $key => $values ) {
        if( in_array( $values[ 'data' ]->get_shipping_class(), $shipping_classes ) )
            $if_exists = true;
    }

    if( $if_exists ) unset( $rates['free_shipping:7'] );

    return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_class_is_in_cart', 10, 2 );

How can I disable shipping for a product if the selected shipping country is not Italy?

aynber
  • 22,380
  • 8
  • 50
  • 63
Leon
  • 141
  • 1
  • 3
  • 13

1 Answers1

8

Note: This code is made to work in Woocommerce 3+ (but not in very old version 2.3)

Your question is not so clear… so you have mainly 2 options (and checking add to cart at the end for both options, when customer is registered, when shipping country is detected or has been set in cart or checkout):

OPTION 1 - Instead of removing shipping methods for a product that is not shippable for all other countries than Italy, you should better remove the related cart item displaying a custom notice… You will have to define the product Ids in the function that are only shippable in Italy:

add_action( 'woocommerce_before_calculate_totals', 'checking_and_removing_items', 10, 1 );
function checking_and_removing_items( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $custome_shipping_country = WC()->customer->get_shipping_country();

    if( empty($custome_shipping_country) ){
        $package = WC()->shipping->get_packages()[0];
        if( ! isset($package['destination']['country']) ) return;
        $custome_shipping_country = $package['destination']['country'];
    }

    // Only for NON Italians customers
    if( $custome_shipping_country == 'IT' ) return;

    // ==> HERE set your product IDs (ITALY ONLY)
    $products_ids = array(37, 57);

    // Iterate through each cart item
    $found = false;
    foreach( $cart->get_cart() as $cart_item_key => $cart_item )
        if( in_array( $cart_item['data']->get_id(), $products_ids ) ){
            $found = true;
            $cart->remove_cart_item( $cart_item_key ); // remove item
        }

    if( $found ){
         // Custom notice
         wc_clear_notices();
         wc_add_notice('Some products are not shippable to your country and have been removed', 'notice');
    }
}

Code goes in function.php file of your active child theme (active theme).

Add to cart validation feature is at the end…


OPTION 2 - Removing shipping methods for a product that is not shippable for all other countries than Italy and displaying a custom error notice… You will have to define the product Ids in the function that are only shippable in Italy:

add_filter( 'woocommerce_package_rates', 'disable_shipping_methods', 20, 2 );
function disable_shipping_methods( $rates, $package ) {
    if( ! ( isset($package['destination']['country']) && isset($package['contents']) ) )
        return $rates;

    // Only for NON Italians customers
    if( $package['destination']['country'] == 'IT' ) return $rates;

    // ==> HERE set your product IDs (ITALY ONLY)
    $products_ids = array(37, 57);

    // Loop through cart items and checking
    $found = false;
    foreach( $package['contents'] as $item )
        if( in_array( $item['data']->get_id(), $products_ids ) ){
            $found = true;
            break;
        }

    if( ! $found ) return $rates; // If nothing is found: We EXIT

    foreach( $rates as $rate_id => $rate )
        unset($rates[$rate_id]); // Removing all shipping methods

    // Custom notice
    wc_clear_notices();
    wc_add_notice('Some products are only shippable for Italy', 'error');

    return $rates;
}

Code goes in function.php file of your active child theme (active theme).


Add to cart validation feature with a custom notice (for both options).

You will have to define the product Ids in the function that are only shippable in Italy.

add_filter( 'woocommerce_add_to_cart_validation', 'avoid_products_for_non_italian', 20, 3 );
function avoid_products_for_non_italian( $passed, $product_id, $quantity ) {

    $custome_shipping_country = WC()->customer->get_shipping_country();

    if( empty($custome_shipping_country) ){
        $package = WC()->shipping->get_packages()[0];
        if( ! isset($package['destination']['country']) ) return $passed;
        $custome_shipping_country = $package['destination']['country'];
    }

    // Only for NON Italians customers
    if( $custome_shipping_country == 'IT' ) return $passed;

    // ==> HERE set your product IDs (ITALY ONLY)
    $products_ids = array(37, 57);

    // The condition
    if( in_array( $product_id, $products_ids ) ){
        $passed = false;
        wc_add_notice( 'This product is only shippable for Italy.', 'error' );
    }
    return $passed;
}

Code goes in function.php file of your active child theme (active theme).

All code is tested and works for Woocommerce version 3+ (may be 2.6.x too)

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    For option 1, is it possible to exclude a whole category instead of products? – Najm Aldekhel Mar 09 '23 at 09:54
  • LoicTheAztec great and helpful answer... I see your name popping up all over the woo questions, so I assume you might work for them! And yes, @NajmAldekhel question, I believe you can replace $products_id with has_term (and add your category name e.g. 'books') see here: https://stackoverflow.com/a/50963409/1319778 – SolaceBeforeDawn Jul 14 '23 at 01:01
  • Or this one : https://stackoverflow.com/a/48535007/1319778 – SolaceBeforeDawn Jul 14 '23 at 01:20
  • 1
    Sorry, you are completely wrong, I don't work for them... – LoicTheAztec Jul 14 '23 at 01:22
  • No offence meant!! I wasn't implying anything sinister, only that you're prolific and wanted to thank you for your contributions, which are always very high quality. – SolaceBeforeDawn Jul 14 '23 at 04:42
  • @SolaceBeforeDawn Thank you :), I remember, I was a real novice when I stated here asking my first question that I finished answering myself. – LoicTheAztec Jul 14 '23 at 07:46