skip to Main Content

How would I adjust this to change country available in the Checkout page by Product Category and not specific Products?

limit shippable country by product category

I’m attempting this:

/* HIDE U.S. as a country destination if category is "Canada Only" */

add_filter( 'woocommerce_countries', 'products_disable_country', 10, 1 );
function products_disable_country( $countries ) {

    if( is_cart() || is_checkout() ) {
    
        $product_cat = array(195);
    
        foreach( WC()->cart->get_cart() as $item ){
            if( in_array( $item['product_cat'], $product_cat ) ){
                unset($countries["US"]);
                return $countries;
            }
        }
    }
    
    return $countries;

}

But no dice yet…

Used this answer code as a base:
Remove a country from allowed countries when specific products are in WooCommerce cart


Edit (Added some screenshots):

Product is Lemon Tarts, categorized as "Canada Only":.

enter image description here

Category ID for that is 195:

enter image description here

Checkout still shows US as an option in the shipping section:

enter image description here

2

Answers


  1. Update (replaced initial "woocommerce_countries" hook)

    To check if a product (or an item) is assigned to a product category term, you should use has_term() conditional WordPress function like:

    add_filter( 'woocommerce_countries_allowed_countries', 'allowed_countries_for_product_category', 10, 1 );
    add_filter( 'woocommerce_countries_shipping_countries', 'allowed_countries_for_product_category', 10, 1 ); // Optional if needed
    function allowed_countries_for_product_category( $countries ) {
        // Only on cart and checkout pages
        if( is_cart() || ( is_checkout() && !is_wc_endpoint_url() ) ) {
            $country_code   = 'US'; // Define the targeted country code to disable
            $targeted_terms = array(195); // Define your category(ies) (terms IDs, slugs or names)
            $category_found = false; // Initializing
    
            // Loop through cart items
            foreach ( WC()->cart->get_cart() as $item ) {
                // Check if one of the targeted categories is assigned
                if( has_term($targeted_terms, 'product_cat', $item['product_id']) ) {
                    $category_found = true; // Tag as found
                    break; // Stop the loop
                }
            }
    
            // Remove the targeted country if any targeted category is found
            if ( $category_found && isset($countries[$country_code]) ){
                unset($countries[$country_code]);
            }
        }
        return $countries;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and work.

    Related: Remove a country from allowed countries when specific products are in WooCommerce cart

    Login or Signup to reply.
  2. Here’s how you can implement this:

    Step 1: Hook into the woocommerce_countries_allowed_countries Filter
    You will use the woocommerce_countries_allowed_countries filter to remove a country from the allowed countries list when a specific product category is present in the cart.

    Step 2: Check the Cart for the Specific Product Category
    You’ll loop through the items in the cart to check if any of them belong to the specific category you’re interested in.

    Step 3: Remove the Country
    If the category is found in the cart, you remove the country from the allowed countries array.

    add_filter( 'woocommerce_countries_allowed_countries', 'remove_country_for_specific_category' );
    add_filter( 'woocommerce_countries_shipping_countries', 'remove_country_for_specific_category' );
    
    function remove_country_for_specific_category( $countries ) {
        // Define the category you want to check for
        $category_slug = 'your-category-slug'; // Replace with your category slug
    
        // Define the country code to remove (e.g., 'US' for the United States)
        $country_to_remove = 'US';
    
        // Check if the cart contains a product from the specific category
        if ( is_cart_contains_category( $category_slug ) ) {
            // Remove the country from the allowed countries list
            if ( isset( $countries[ $country_to_remove ] ) ) {
                unset( $countries[ $country_to_remove ] );
            }
        }
    
        return $countries;
    }
    
    function is_cart_contains_category( $category_slug ) {
        // Loop through cart items and check for the category
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            if ( has_term( $category_slug, 'product_cat', $cart_item['product_id'] ) ) {
                return true;
            }
        }
    
        return false;
    }
    

    woocommerce_countries_allowed_countries Filter: This filter modifies the list of allowed billing countries. The second filter, woocommerce_countries_shipping_countries, modifies the shipping countries list.

    Category Check: The function is_cart_contains_category() loops through all items in the cart and checks if any belong to the specified category (your-category-slug).

    Country Removal: If a product from the specified category is found in the cart, the country (US in this example) is removed from the list of allowed countries.

    Customization:
    $category_slug: Replace ‘your-category-slug’ with the actual slug of the product category you want to check.

    $country_to_remove: Replace ‘US’ with the ISO 3166-1 alpha-2 code of the country you want to remove.

    Use Case:
    Restrict Shipping: This could be useful if certain products cannot be shipped to specific countries due to regulations, shipping restrictions, or other reasons.
    This code should be added to your theme’s functions.php file

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search