2

I am trying to show only limited countries in WooComerce checkout for particular products only.

I am successfully able to get the cart product id but i cannot get it outside the function.

// hide countries

function get_cart_product_id() {
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];
        if(!empty($product)){
        $live_pro = $product->get_id();
        }
    }
    return $live_pro;
    global $live_pro;
}

global $live_pro;
echo $live_pro;   // Donesn't echo anything here. Below if also not working

if ($live_pro == 435925 || $live_pro == 435929 || $live_pro == 435930 || $live_pro == 435931 || $live_pro == 435932 ) {
  add_filter( 'woocommerce_countries', 'bbloomer_custom_woocommerce_countries' );
}

function bbloomer_custom_woocommerce_countries( $country ) {
$country = array(
'ES' => 'Spain',
'PT' => 'Portugal',
'FR' => 'France',
);
return $country;
}

Any advice to help me find the solution?

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50

2 Answers2

2

To show/hide certain countries on WooCommerce checkout for particular products in cart, you can use the woocommerce_countries_allowed_countries filter hook.

Either you indicate which country (codes) you want to remove:

function filter_woocommerce_countries_allowed_countries( $countries ) {
    // Cart or checkout page
    if ( is_cart() || is_checkout() ) {     
        // The targeted product ids
        $targeted_ids = array( 30, 815 );

        // Flag
        $found = false;
        
        if ( WC()->cart ) {         
            // Loop through cart items
            foreach ( WC()->cart->get_cart() as $cart_item ) {
                if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
                    $found = true;
                    break;
                }
            }
        }
        
        // True
        if ( $found ) {
            // Remove
            unset( $countries[ 'NL' ] );
            unset( $countries[ 'FR' ] );
        }
    }

    // Return
    return $countries;
}
add_filter( 'woocommerce_countries_allowed_countries', 'filter_woocommerce_countries_allowed_countries', 10, 1 );

OR

The reverse, and indicate which country (codes) you want to keep

function filter_woocommerce_countries_allowed_countries( $countries ) { 
    // Cart or checkout page
    if ( is_cart() || is_checkout() ) {     
        // The targeted product ids
        $targeted_ids = array( 30, 815 );
    
        // Country codes you want to show
        $show_countries = array( 'BE', 'NL', 'FR', 'ES' );

        // Flag
        $found = false;
        
        if ( WC()->cart ) {         
            // Loop through cart items
            foreach ( WC()->cart->get_cart() as $cart_item ) {
                if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
                    $found = true;
                    break;
                }
            }
        }
        
        // True
        if ( $found ) {
            // Loop through country codes
            foreach ( $countries as $key => $country ) {
                // NOT found
                if ( ! in_array( $key, $show_countries ) ) {
                    // Remove
                    unset( $countries[$key] );
                }
            }
        }
    }

    // Return
    return $countries;
}
add_filter( 'woocommerce_countries_allowed_countries', 'filter_woocommerce_countries_allowed_countries', 10, 1 );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
0

To only show certain countries on the WooCommerce checkout page, you can follow these steps:

  • Log in to your WordPress admin dashboard.

  • Navigate to "WooCommerce" and click on "Settings."

  • In the "Settings" tab, click on the "General" sub-tab.

  • Scroll down to the "Default customer location" section.

  • Select the desired option from the "Sell to specific countries" drop-down menu.
    "All allowed countries" will show a list of all countries.
    "Specific countries" will allow you to select specific countries.

  • If you selected "Specific countries," click on the "Specific Countries" button that appears.

  • A list of countries will be displayed. Check the boxes next to the countries you want to show on the checkout page.

  • Click the "Save changes" button to save your settings.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mihkel
  • 1