在 WooCommerce 结账时为购物车中的特定产品显示某些国家/地区

时间:2021-06-24 08:00:59

标签: php wordpress woocommerce checkout countries

我试图在 WooComerce 结帐中仅显示特定产品的有限国家/地区。

我能够成功获取购物车产品 ID,但无法在函数之外获取它。

// 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;
}

有什么建议可以帮助我找到解决方案吗?

1 个答案:

答案 0 :(得分:2)

要在 WooCommerce 结账时为购物车中的特定产品显示/隐藏某些国家/地区,您可以使用 woocommerce_countries_allowed_countries 过滤器钩子。

或者您指明要删除的国家/地区(代码):

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

相反,并指明您要保留哪个国家(代码)

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 );
相关问题