在 Woocommerce 中根据 IP 地址(地理位置)更改添加到购物车按钮
Change add to cart button based on IP-adress (GeoLocation) in Woocommerce
我对我目前正在经营的 WooCommerce 商店有疑问。该商店仅包含两种语言,荷兰语和英语。
当来自波兰的某人访问英文版的网上商店然后导航到 WooCommerce 产品页面时,是否有可能不显示 "Add to Cart" 选项,而是显示一个带有另一个 link 的不同按钮基于 IP 地址(地理位置)?
编辑
好的,我设法让它工作了,但你的例子不行:
// Wijzigingen Links en teksten knoppen toevoegen
function custom_product_button(){
// GEOLocatie aanroepen en verschillende distrubiteurs toevoegen
$geoip = geoip_detect2_get_info_from_current_ip();
$country = $geoip->raw[ 'country' ][ 'iso_code' ];
$button_text = __( "To distributor's website", "woocommerce" );
$button_usa = 'https://google.com';
$button_singapore = 'https://www.google.com.sg';
// Tonen van buttons met verschillende linkjes
?>
<form class="cart">
<input type="submit" value="<?php echo $button_text; ?>" onClick="window.open('<?php if ( 'US' === $country ) {echo $button_usa;}if ( 'SG' === $country ) {echo $button_singapore;}?>');"class="single_add_to_cart_button button alt">
</form>
<?php
}
// Vervangen van de button op single product pagina
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
global $product;
$geoip = geoip_detect2_get_info_from_current_ip();
$country = $geoip->raw[ 'country' ][ 'iso_code' ];
if ( 'US' === $country || 'SG' === $country)
{
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'custom_product_button', 30 );
}
}
// Vervangen van de button op loop pagina en categorie / archief pagina
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
$geoip = geoip_detect2_get_info_from_current_ip();
$country = $geoip->raw[ 'country' ][ 'iso_code' ];
if ( 'US' === $country || 'SG' === $country) {
$button_text = __( "View product", "woocommerce" );
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
return $button;
}
也许不是最好的编码片段,但它与插件 GeoIP 检测结合使用。
您的编码看起来好多了,但似乎不起作用。没有错误,总是添加到购物车按钮。
Updated: on April 21th, 2018
Woocommerce 有一个启用地理定位的工具(和WC_Geolocation
相关class) 默认。
下面的第一个在自定义条件函数中使用该地理位置,您将在其中定义将启用演示按钮的相关国家/地区 。
在该函数中,您将在数组中设置 所有相关的国家代码 (我定义了 'US' 和 'SG' 只是为了测试目的)。
代码:
// Updated: check for the defined country codes based over user IP geolocation country code
function country_geo_ip_check(){
// ==> HERE define the countries codes in the array
$non_allowed_countries = array('US', 'SG');
// 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 in_array( $user_geolocation['country'], $non_allowed_countries ) ? false : true;
}
// Shop and other archives pages (replacing add to cart by a linked button to the product)
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
if ( country_geo_ip_check() ) return $button; // Exit for non defined countries
if ( $product->is_type( 'variable' ) ) return $button; // Excluding variable products
$button_text = __( "View product", "woocommerce" );
$button_link = $product->get_permalink();
return '<a class="button" href="' . $button_link . '">' . $button_text . '</a>';;
}
// Single producct pages
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
global $product;
if ( country_geo_ip_check() ) return; // Exit for non defined countries
// For variable product types (keeping attribute select fields)
if( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_product_summary', 'custom_demo_button', 20 );
}
// For all other product types
else {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'custom_demo_button', 30 );
}
}
// Your custom button replacement
function custom_demo_button() {
global $post, $product;
$style = 'style="padding-right: 0.75em;padding-left: 0.75em;margin-left: 8px; background-color: #0ebc30;"';
$button_text = __("View Demo", "woocommerce");
// Get demo Url
if( function_exists('get_field') )
$url_demo = get_field( "url_demo" );
if( ! isset($url_demo) ){ // If the Url is not defined
$button_text = __("Missing URL", "woocommerce");
$style = 'style="color: red; background-color: white; border: solid 1px red"';
$url_demo = '#';
}
// Output
echo '<a href="'.$url_demo.'" target="_blank" class="button demo_button"'.$style.'>'.$button_text.'</a>';
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。
我对我目前正在经营的 WooCommerce 商店有疑问。该商店仅包含两种语言,荷兰语和英语。
当来自波兰的某人访问英文版的网上商店然后导航到 WooCommerce 产品页面时,是否有可能不显示 "Add to Cart" 选项,而是显示一个带有另一个 link 的不同按钮基于 IP 地址(地理位置)?
编辑
好的,我设法让它工作了,但你的例子不行:
// Wijzigingen Links en teksten knoppen toevoegen
function custom_product_button(){
// GEOLocatie aanroepen en verschillende distrubiteurs toevoegen
$geoip = geoip_detect2_get_info_from_current_ip();
$country = $geoip->raw[ 'country' ][ 'iso_code' ];
$button_text = __( "To distributor's website", "woocommerce" );
$button_usa = 'https://google.com';
$button_singapore = 'https://www.google.com.sg';
// Tonen van buttons met verschillende linkjes
?>
<form class="cart">
<input type="submit" value="<?php echo $button_text; ?>" onClick="window.open('<?php if ( 'US' === $country ) {echo $button_usa;}if ( 'SG' === $country ) {echo $button_singapore;}?>');"class="single_add_to_cart_button button alt">
</form>
<?php
}
// Vervangen van de button op single product pagina
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
global $product;
$geoip = geoip_detect2_get_info_from_current_ip();
$country = $geoip->raw[ 'country' ][ 'iso_code' ];
if ( 'US' === $country || 'SG' === $country)
{
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'custom_product_button', 30 );
}
}
// Vervangen van de button op loop pagina en categorie / archief pagina
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
$geoip = geoip_detect2_get_info_from_current_ip();
$country = $geoip->raw[ 'country' ][ 'iso_code' ];
if ( 'US' === $country || 'SG' === $country) {
$button_text = __( "View product", "woocommerce" );
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
return $button;
}
也许不是最好的编码片段,但它与插件 GeoIP 检测结合使用。
您的编码看起来好多了,但似乎不起作用。没有错误,总是添加到购物车按钮。
Updated: on April 21th, 2018
Woocommerce 有一个启用地理定位的工具(和WC_Geolocation
相关class) 默认。
下面的第一个在自定义条件函数中使用该地理位置,您将在其中定义将启用演示按钮的相关国家/地区 。
在该函数中,您将在数组中设置 所有相关的国家代码 (我定义了 'US' 和 'SG' 只是为了测试目的)。
代码:
// Updated: check for the defined country codes based over user IP geolocation country code
function country_geo_ip_check(){
// ==> HERE define the countries codes in the array
$non_allowed_countries = array('US', 'SG');
// 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 in_array( $user_geolocation['country'], $non_allowed_countries ) ? false : true;
}
// Shop and other archives pages (replacing add to cart by a linked button to the product)
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
if ( country_geo_ip_check() ) return $button; // Exit for non defined countries
if ( $product->is_type( 'variable' ) ) return $button; // Excluding variable products
$button_text = __( "View product", "woocommerce" );
$button_link = $product->get_permalink();
return '<a class="button" href="' . $button_link . '">' . $button_text . '</a>';;
}
// Single producct pages
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
global $product;
if ( country_geo_ip_check() ) return; // Exit for non defined countries
// For variable product types (keeping attribute select fields)
if( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_product_summary', 'custom_demo_button', 20 );
}
// For all other product types
else {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'custom_demo_button', 30 );
}
}
// Your custom button replacement
function custom_demo_button() {
global $post, $product;
$style = 'style="padding-right: 0.75em;padding-left: 0.75em;margin-left: 8px; background-color: #0ebc30;"';
$button_text = __("View Demo", "woocommerce");
// Get demo Url
if( function_exists('get_field') )
$url_demo = get_field( "url_demo" );
if( ! isset($url_demo) ){ // If the Url is not defined
$button_text = __("Missing URL", "woocommerce");
$style = 'style="color: red; background-color: white; border: solid 1px red"';
$url_demo = '#';
}
// Output
echo '<a href="'.$url_demo.'" target="_blank" class="button demo_button"'.$style.'>'.$button_text.'</a>';
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。