Woocommerce根据指定IP隐藏指定产品

本文作者: Fengjiajun 评论数量: 0

在此示例中,如果用户的IP属于美国IP地址,则将隐藏ID为21和32的产品:

function fjj_hide_product_if_country_new( $q, $query ) {
    if ( is_admin() ) return;
    $location = WC_Geolocation::geolocate_ip();
    $hide_products = array( 21, 32 );   
    $country = $location['country'];   
    if ( $country === "US" ) {
        $q->set( 'post__not_in', $hide_products );
    } 
}
add_action( 'woocommerce_product_query', 'fjj_hide_product_if_country_new', 9999, 2 );

 


专注 Wordpress Woocommerce 独立站开发&谷歌广告推广 8 年

免费提供WordPress建站以及谷歌广告推广相关咨询服务

This will close in 0 seconds