Search code examples
phpwordpresswoocommercecheckoutcountries

Show certain countries on WooCommerce checkout for particular products in cart


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?


Solution

  • 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 );