<bdo id='70r6c'></bdo><ul id='70r6c'></ul>
<i id='70r6c'><tr id='70r6c'><dt id='70r6c'><q id='70r6c'><span id='70r6c'><b id='70r6c'><form id='70r6c'><ins id='70r6c'></ins><ul id='70r6c'></ul><sub id='70r6c'></sub></form><legend id='70r6c'></legend><bdo id='70r6c'><pre id='70r6c'><center id='70r6c'></center></pre></bdo></b><th id='70r6c'></th></span></q></dt></tr></i><div id='70r6c'><tfoot id='70r6c'></tfoot><dl id='70r6c'><fieldset id='70r6c'></fieldset></dl></div>

    1. <small id='70r6c'></small><noframes id='70r6c'>

    2. <tfoot id='70r6c'></tfoot>
    3. <legend id='70r6c'><style id='70r6c'><dir id='70r6c'><q id='70r6c'></q></dir></style></legend>

      将 Woocommerce 商店变成地理定位国家的目录?

      Turn Woocommerce shop into catalog for geolocated countries?(将 Woocommerce 商店变成地理定位国家的目录?)

          <legend id='JmUIk'><style id='JmUIk'><dir id='JmUIk'><q id='JmUIk'></q></dir></style></legend>

                <bdo id='JmUIk'></bdo><ul id='JmUIk'></ul>

                <i id='JmUIk'><tr id='JmUIk'><dt id='JmUIk'><q id='JmUIk'><span id='JmUIk'><b id='JmUIk'><form id='JmUIk'><ins id='JmUIk'></ins><ul id='JmUIk'></ul><sub id='JmUIk'></sub></form><legend id='JmUIk'></legend><bdo id='JmUIk'><pre id='JmUIk'><center id='JmUIk'></center></pre></bdo></b><th id='JmUIk'></th></span></q></dt></tr></i><div id='JmUIk'><tfoot id='JmUIk'></tfoot><dl id='JmUIk'><fieldset id='JmUIk'></fieldset></dl></div>
                <tfoot id='JmUIk'></tfoot>
                  <tbody id='JmUIk'></tbody>

                <small id='JmUIk'></small><noframes id='JmUIk'>

              • 本文介绍了将 Woocommerce 商店变成地理定位国家的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试将我的 woocommerce 商店转换为取决于国家/地区的目录模式.我只想在美国显示产品的价格并添加到购物车功能.我正在尝试通过使用名为GeoIP Detection"的插件来实现这一点,其中包含以下内容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:

                • 添加内联 CSS 除外Woocommerce 中的特定国家/地区
                • 在 Woocommerce 中禁用基于用户国家地理 IP 的支付网关
                • 更改Woocommerce中基于IP地址(地理位置)的添加到购物车按钮

                这篇关于将 Woocommerce 商店变成地理定位国家的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How do I parse XML containing custom namespaces using SimpleXML?(如何使用 SimpleXML 解析包含自定义命名空间的 XML?)
                SimpleXML SOAP response Namespace issues(SimpleXML SOAP 响应命名空间问题)
                Problems with PHP namespaces and built-in classes, how to fix?(PHP 命名空间和内置类的问题,如何解决?)
                Use php namespace inside function(在函数内部使用 php 命名空间)
                unexpected #39;use#39; (T_USE) when trying to use composer(尝试使用作曲家时意外的“使用(T_USE))
                PHP adding custom namespace using autoloader from composer(PHP使用来自作曲家的自动加载器添加自定义命名空间)
                <tfoot id='XawKr'></tfoot>

                  • <i id='XawKr'><tr id='XawKr'><dt id='XawKr'><q id='XawKr'><span id='XawKr'><b id='XawKr'><form id='XawKr'><ins id='XawKr'></ins><ul id='XawKr'></ul><sub id='XawKr'></sub></form><legend id='XawKr'></legend><bdo id='XawKr'><pre id='XawKr'><center id='XawKr'></center></pre></bdo></b><th id='XawKr'></th></span></q></dt></tr></i><div id='XawKr'><tfoot id='XawKr'></tfoot><dl id='XawKr'><fieldset id='XawKr'></fieldset></dl></div>

                          <tbody id='XawKr'></tbody>
                        <legend id='XawKr'><style id='XawKr'><dir id='XawKr'><q id='XawKr'></q></dir></style></legend>

                        <small id='XawKr'></small><noframes id='XawKr'>

                        • <bdo id='XawKr'></bdo><ul id='XawKr'></ul>