I have a question about a WooCommerce shop I am currently working on. The shop only contains only two languages, Dutch and English.
Is it possible when somebody from Poland visits the English version of the webshop and then navigates to the WooCommerce Products page it does not show the "Add to Cart" option but displays a different button with another link based on IP-adres (Geo-Location)?
EDIT
Ok, I managed to get it to work but not with your example:
// 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;
}
Maybe not the best coded piece but it is working in combination with the Plug-in GeoIP Detection.
Your coding looks much better but doen't seem te work. No errors just always the add to cart button.
Woocommerce has a geolocation enabled tool (and
WC_Geolocation
related class) by default.The first below uses that geolocation in a custom conditional function, where you will defined the related countries that are going to enabled your demo button.
In that function you will set all the related country codes in the array (I have defined 'US' and 'SG' just for testing purpose).
The code:
Code goes in function.php file of your active child theme (or active theme). Tested and work.