問題描述
我已經使用 woocommerce 開發了一個目錄,但是由于我無法控制的原因,我需要能夠對從英國以外訪問該網站的用戶隱藏產品價格.
我找到了允許我根據訪問者位置更改產品價格的插件,但沒有任何東西可以讓我隱藏價格.
是否有我遺漏的插件或我可以添加到 woocommerce 文件中以實現此目的的任何內容?
以下內容將隱藏英國以外的價格,基于客戶地理定位的國家:
add_filter( 'woocommerce_get_price_html', 'country_geolocated_based_hide_price', 10, 2 );函數 country_geolocated_based_hide_price( $price, $product ) {//獲取 WC_Geolocation 對象類的實例$geo_instance = new WC_Geolocation();//獲取地理定位的用戶地理數據.$user_geodata = $geo_instance->geolocate_ip();//獲取當前用戶的 GeoIP 國家$country = $user_geodata['country'];返回 $country !== 'GB' ?'' : $價格;}
代碼位于活動子主題(或活動主題)的 function.php 文件中.經測試有效.
<小時>如果您只想為未登錄的客戶啟用該地理定位功能,請使用以下命令:
add_filter( 'woocommerce_get_price_html', 'country_geolocated_based_hide_price', 10, 2 );函數 country_geolocated_based_hide_price( $price, $product ) {如果(get_current_user_id()> 0){$country = WC()->customer->get_billing_country();} 別的 {//獲取 WC_Geolocation 對象類的實例$geo_instance = new WC_Geolocation();//獲取地理定位的用戶地理數據.$user_geodata = $geo_instance->geolocate_ip();//獲取當前用戶的 GeoIP 國家$country = $user_geodata['country'];}返回 $country !== 'GB' ?'' : $價格;}
<塊引用>
此代碼的更新版本在此答案上可用,避免了后端錯誤.
我在開始時添加了函數:
if ( is admin() ) return $price;
代碼位于活動子主題(或活動主題)的 function.php 文件中.經測試有效.
I've developed a catalogue using woocommerce, however I need to be able to hide product prices from users who are visiting the site from outside of the UK due to reasons outside of my control.
I've found plugins that allow me to change product prices based on visitor location, but nothing allows me to hide the prices.
Is there any plugins that I've missed or anything I can add in to the woocommerce files to achieve this?
The following will hide prices outside United kingdom based on customer geolocated country:
add_filter( 'woocommerce_get_price_html', 'country_geolocated_based_hide_price', 10, 2 );
function country_geolocated_based_hide_price( $price, $product ) {
// 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;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
If you want to enable that geolocated feature only for unlogged customers, use the following:
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;
}
An updated version of this code is available on this answer avoiding a backend bug.
I have added in the function on start:
if ( is admin() ) return $price;
Code goes in function.php file of your active child theme (or active theme). Tested and works.
這篇關于在 Woocommerce 中僅顯示特定客戶所在國家/地區的價格的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!