I'm working with WooCommerce and I am trying to develop a way to perform price calculation based in two factors:
- location of the customer by city
- category
I was able to change price based on the location of the customer but if I try to filter by location and category at the same time it does not work. To perform the filter by category I add this if statement:
if( has_term( array('nike'), 'product_cat' ,$product->ID) )
But I don't understand why this does not work.
Here is the code:
$userInfo = geoip_detect2_get_info_from_current_ip();
$countryCode = $userInfo->city->name;
switch ($countryCode) {
case 'Madrid':
if (has_term(array('nike') , 'product_cat', $product->ID)) {
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':
if (has_term(array('adidas') , 'product_cat', $product->ID)) {
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;
}