[SOLVED] How would I adjust this to change country available in the Checkout page by Product Category and not specific Products? 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":. Category ID for that is 195: Checkout still shows US as an option in the shipping section: Thanks Loic! Your final update worked perfectly, see below: You're a star!
Jese Leos
August 19, 2024
Verified user
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
Jese Leos
August 19, 2024
Verified user
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 ); 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. For "Ship to", targeting shipping countries only, use additionally: add_filter( 'woocommerce_countries_shipping_countries', 'allowed_countries_for_product_category', 10, 1 ); Related: Remove a country from allowed countries when specific products are in WooCommerce cart