【发布时间】:2021-02-27 10:11:11
【问题描述】:
通过下面的代码,我可以使用 WooCommerce WC_Geolocation 类隐藏特定国家/地区的特定产品:
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 );
}
}
代码运行良好,并根据商店/类别中的地理定位国家/地区隐藏产品,并在前端搜索结果。
但如果有人使用直接链接,产品仍然可见。如何避免客户也直接访问特定的单品页面?
【问题讨论】:
标签: php wordpress woocommerce geolocation product