Woocommerce中基于访问者位置后端错误隐藏价格 [英] Hiding prices based on visitor location backend bug in Woocommerce

查看:106
本文介绍了Woocommerce中基于访问者位置后端错误隐藏价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的上一个问题中,我询问了如何向英国以外的游客隐藏价格.

In my last question I asked how to hide prices to visitors outside the UK.

根据我成功使用此代码的答案

Based off of an answer I used this code successfully

add_filter( 'woocommerce_get_price_html', 'country_geolocated_based_hide_price', 10, 2 );
function country_geolocated_based_hide_price( $price, $product ) {
    if( get_current_user_id() > 0 ) {
        $country = WC()->customer->get_billing_country();
    } else {
        // Get an instance of the WC_Geolocation object class
        $geo_instance  = new WC_Geolocation();
        // Get geolocated user geo data.
        $user_geodata = $geo_instance->geolocate_ip();
        // Get current user GeoIP Country
        $country = $user_geodata['country'];
    }
    return $country !== 'GB' ? '' : $price;
}

这按预期工作,但是当我尝试在管理区域编辑我的产品时,我在每个产品的价格列中收到此错误:

This works as expected, however when I try to edit my products in the admin area I get this error in the price column of every product:

致命错误:未捕获错误:调用成员函数 get_billing_country() on null in/var/sites/o/oxfordriderwear.com/public_html/wp-content/themes/storefront/functions.php:61堆栈跟踪:#0/var/sites/o/oxfordriderwear.com/public_html/wp-includes/class-wp-hook.php(286):country_geolocated_based_hide_price('apply_filters('get_price_html() #4/var/sites/o/oxfordriderwear.com/public_html/wp-content/plugins/woocommerce/includes/admin/list-tables/abstract-class-wc-admin-list-table.php(261):卫生间在/var/sites/o/oxfordriderwear.com/public_html/wp-content/themes/storefront/functions.php在第 61 行

我使用的代码中是否有不正确的地方,或者我需要添加一些东西来解决这个问题,以便我的管理区域正常显示?

Is there something incorrect in the code i've used, or something I need to add to fix this issue so that my admin area displays as normal?

推荐答案

为了避免这个问题,我们可以在后端返回格式化后的价格:

To avoid this problem we can return the formatted price on backend with the following:

add_filter( 'woocommerce_get_price_html', 'country_geolocated_based_hide_price', 10, 2 );
function country_geolocated_based_hide_price( $price, $product ) {
    // Not on backend
    if( is_admin() ) 
        return $price;

    if( get_current_user_id() > 0 ) {
        $country = WC()->customer->get_billing_country();
    } else {
        // Get an instance of the WC_Geolocation object class
        $geo_instance  = new WC_Geolocation();
        // Get geolocated user geo data.
        $user_geodata = $geo_instance->geolocate_ip();
        // Get current user GeoIP Country
        $country = $user_geodata['country'];
    }
    return $country !== 'GB' ? '' : $price;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

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

没有更多的错误.

这篇关于Woocommerce中基于访问者位置后端错误隐藏价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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