将Woocommerce商店转变为地理位置所在国家/地区的目录? [英] Turn Woocommerce shop into catalog for geolocated countries?

查看:225
本文介绍了将Woocommerce商店转变为地理位置所在国家/地区的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将woocommerce商店转变为目录模式,具体取决于国家/地区.我只想显示产品的价格并在美国添加到购物车功能.我正在尝试通过使用名为" GeoIP检测的插件来实现此目标,并将以下内容PHP中的代码:

I'm trying to turn my woocommerce shop into catalog mode depends on country.I only want to show products' price and add to cart function in United States. I'm trying to achieve this by using a plugin called "GeoIP Detection" with the following code in the PHP:

/* Disable purchasing of products if the country is not United States*/
add_filter('woocommerce_is_purchasable', 'flatsome_woocommerce_is_purchasable', 10, 2);
function flatsome_woocommerce_is_purchasable($is_purchasable, $product) {
    $geoip = geoip_detect2_get_info_from_current_ip();
    $country = $geoip->raw[ 'country' ][ 'iso_code' ];
    if ( 'US' == $country ) { 
        return true;
        }
}
/*filter out prices based on country.*/
    add_filter( 'woocommerce_variable_sale_price_html', 'bdd_remove_prices', 10, 2 );
    add_filter( 'woocommerce_variable_price_html', 'bdd_remove_prices', 10, 2 );
    add_filter( 'woocommerce_get_price_html', 'bdd_remove_prices', 10, 2 );
function bdd_remove_prices( $price, $product ) {
    $geoip = geoip_detect2_get_info_from_current_ip();
    $country = $geoip->raw[ 'country' ][ 'iso_code' ];
    if ( 'US' !== $country )
    {$price = '';}
    return $price;
}

此代码可以正常工作,但无法正常运行.有时我的其他国家的朋友仍然可以看到价格,而且当我自己浏览网站时,我发现一个奇怪的错误,想知道这是否只是缓存问题.

This code does work but not fully functioning as I expected. Sometimes my friends from other countries can still see the price, and also I found some odd bug when I browse the website myself, wondering if it's just cache issue.

有人可以帮助我改进此代码吗?这对社区也很有帮助.

Can anyone help me to maybe improve this code? This can be very helpful too for the community.

我对此编码还有一个奇怪的问题:

Also I have an odd question with this coding:

function flatsome_woocommerce_is_purchasable($is_purchasable, $product) {
    $geoip = geoip_detect2_get_info_from_current_ip();
    $country = $geoip->raw[ 'country' ][ 'iso_code' ];
    if ( 'US' == $country ) { 
        return true;
        }
}

当我写信时:

if ( 'US' !== $country ) { 
        return false;
        }

这行不通,所以我也很好奇为什么.

It won't work, so I'm also curious why.

推荐答案

2020年5月:在WooCommerce 4+上经过测试并且对于可变产品也很完美

May 2020: Tested and works perfectly for variable products too on WooCommerce 4+

对于WooCommerce,您不需要为此使用任何插件

WooCommerce已经拥有专用的 WC_Geolocation 会做得更好的类.

WooCommerce has already the dedicated WC_Geolocation class that will do it better.

使用WooCommerce的代码 WC_Geolocation 类代替:

The code using WooCommerce WC_Geolocation class instead:

// Utility function to get geolocated user country based on WooCommerce WC_Geolocation Class
function get_geolocated_user_country(){
   // Get an instance of the WC_Geolocation object class
    $geolocation_instance = new WC_Geolocation();
    // Get user IP
    $user_ip_address = $geolocation_instance->get_ip_address();
    // Get geolocated user IP country code.
    $user_geolocation = $geolocation_instance->geolocate_ip( $user_ip_address );

    return $user_geolocation['country'];
}


// Disable purchasing of products if the country is not United States
add_filter('woocommerce_is_purchasable', 'purchasable_country_based', 10, 2);
function purchasable_country_based( $is_purchasable, $product ) {
    // Enable for "US" only geolocated users country
    if( get_geolocated_user_country() ==='US' )
        return true;
    else
        return false;
}
// Filter out prices based on country
add_filter( 'woocommerce_variable_sale_price_html', 'display_prices_country_based', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'display_prices_country_based', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'display_prices_country_based', 10, 2 );
function display_prices_country_based( $price, $product ) {
    // Enable for "US" only geolocated users country
    if( get_geolocated_user_country() === 'US' )
        return $price;
    else
        return '';
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

现在,当美国客户想要从另一个国家购买商品时,例如他们旅行时,您可能会遇到一些问题……

Now you might face some problems when US customers want to buy from another country, if they are travelling for example…

Woocommerce Geo IP相关答案:

Woocommerce Geo IP related answer:

  • Add inline CSS except for a specific country in Woocommerce
  • Disable payment gateways based on user country geo-ip in Woocommerce
  • Change add to cart button based on IP-adress (GeoLocation) in Woocommerce

这篇关于将Woocommerce商店转变为地理位置所在国家/地区的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆