If statement with geo ip
-
Hi ! i′m a php newbie dealing with woocommerce to show different prices based on de geolocation of the customer , in this case i′m trying to target customers by city . I have this code , it′s works well .
$userInfo = geoip_detect2_get_info_from_current_ip();
$countryCode = $userInfo->city->name;
switch ($countryCode) {case ‘Madrid’:
add_filter(‘woocommerce_get_price’, ‘product_custom_price’, 10, 2);
function product_custom_price($price, $product) {
$custom_price = $product->get_regular_price();
return $custom_price * 1.50;}
break;
case ‘Barcelona’:
add_filter(‘woocommerce_get_price’, ‘product_custom_price’, 10, 2);
function product_custom_price($price, $product) {
$custom_price = $product->get_regular_price();
return $custom_price * 1.25;}
break;
}i′m tryin to filter by city and product category , but i′m really stuck with this , i have this code but does not work.
$userInfo = geoip_detect2_get_info_from_current_ip();
$countryCode = $userInfo->city->name;
$terms = get_the_terms($product->ID, ‘product_cat’);
switch ($countryCode) {
case ‘Madrid’:
foreach ( $terms as $term ) {
if($term->name == ‘nike’) {
add_filter(‘woocommerce_get_price’, ‘product_custom_price’, 10, 2);
function product_custom_price($price, $product) {
$custom_price = $product->get_regular_price();
return $custom_price * 1.5;}
}
}break;
case ‘Barcelona’:foreach ( $terms as $term ) {
if($term->name == ‘adidas’) {
add_filter(‘woocommerce_get_price’, ‘product_custom_price’, 10, 2);
function product_custom_price($price, $product) {
$custom_price = $product->get_regular_price();
return $custom_price * 1.25;}
}
}break;
}
Regards!!
- The topic ‘If statement with geo ip’ is closed to new replies.