避免访问基于 WooCommerce 地理位置国家的特定产品
Avoid accessing specific products based on WooCommerce geolocated country
在下面的代码中,我可以使用 WooCommerce 隐藏特定国家/地区的特定产品 WC_Geolocation
Class:
add_action( 'woocommerce_product_query', 'bbloomer_hide_product_if_country_new', 9999, 2 );
function bbloomer_hide_product_if_country_new( $q, $query ) {
if ( is_admin() ) return;
$location = WC_Geolocation::geolocate_ip();
$hide_products = array( 17550, 32 );
$country = $location['country'];
if ( $country === "US" ) {
$q->set( 'post__not_in', $hide_products );
}
}
该代码运行良好,并根据商店/类别中的地理定位国家和前端的搜索结果隐藏了产品。
但如果有人使用直接 link,产品仍然可见。如何避免客户也直接访问特定的单个产品页面?
用以下代码替换所有代码:
// Conditional custom function: check for geolocated countries (array)
function is_targeted_geo_country( $countries ) {
$location = WC_Geolocation::geolocate_ip();
return in_array( $location['country'], $countries );
}
// Hide specific products from catalog
add_action( 'woocommerce_product_query', 'hide_product_for_geo_countries', 9999, 2 );
function hide_product_for_geo_countries( $q, $query ) {
if ( is_admin() ) return;
$product_ids = array( 17550, 32 ); // Here set the product to hide
if ( is_targeted_geo_country( array('US') ) ) {
$q->set( 'post__not_in', $hide_products );
}
}
// Avoid accessing specific products, redirecting to shop
add_action( 'template_redirect', 'redirect_to_shop_products_for_geo_countries' );
function redirect_to_shop_products_for_geo_countries() {
$product_ids = array( 17550, 32 ); // Here set the products to hide
if ( is_targeted_geo_country( array('US') ) && in_array( get_the_ID(), $product_ids ) ) {
wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
exit();
}
}
在下面的代码中,我可以使用 WooCommerce 隐藏特定国家/地区的特定产品 WC_Geolocation
Class:
add_action( 'woocommerce_product_query', 'bbloomer_hide_product_if_country_new', 9999, 2 );
function bbloomer_hide_product_if_country_new( $q, $query ) {
if ( is_admin() ) return;
$location = WC_Geolocation::geolocate_ip();
$hide_products = array( 17550, 32 );
$country = $location['country'];
if ( $country === "US" ) {
$q->set( 'post__not_in', $hide_products );
}
}
该代码运行良好,并根据商店/类别中的地理定位国家和前端的搜索结果隐藏了产品。
但如果有人使用直接 link,产品仍然可见。如何避免客户也直接访问特定的单个产品页面?
用以下代码替换所有代码:
// Conditional custom function: check for geolocated countries (array)
function is_targeted_geo_country( $countries ) {
$location = WC_Geolocation::geolocate_ip();
return in_array( $location['country'], $countries );
}
// Hide specific products from catalog
add_action( 'woocommerce_product_query', 'hide_product_for_geo_countries', 9999, 2 );
function hide_product_for_geo_countries( $q, $query ) {
if ( is_admin() ) return;
$product_ids = array( 17550, 32 ); // Here set the product to hide
if ( is_targeted_geo_country( array('US') ) ) {
$q->set( 'post__not_in', $hide_products );
}
}
// Avoid accessing specific products, redirecting to shop
add_action( 'template_redirect', 'redirect_to_shop_products_for_geo_countries' );
function redirect_to_shop_products_for_geo_countries() {
$product_ids = array( 17550, 32 ); // Here set the products to hide
if ( is_targeted_geo_country( array('US') ) && in_array( get_the_ID(), $product_ids ) ) {
wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
exit();
}
}