gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 07:28:07 25 4
gpt4 key购买 nike

我试图在 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;
}

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

最佳答案

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

关于php - 在 WooCommerce 结账时为购物车中的特定产品显示某些国家/地区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68111786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com