• Resolved oker

    (@oker)


    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!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Benjamin Pick

    (@benjamin4)

    I don’t give PHP support ?? but to get you started, the problem probably is that you defined the function called “product_custom_price” twice. You need to give it a different name each time. Or (depending on your PHP version) use nameless functions:

    add_filter(‘woocommerce_get_price’, function($price, $product) { ... }, 10, 2);

    • This reply was modified 7 years, 10 months ago by Benjamin Pick.
    Plugin Author Benjamin Pick

    (@benjamin4)

    (Also, as a customer I would be a bit annoyed if I knew you are changing the prices based on location data … The location data ist not 100% accurate, so they might see different prices depending on whether they look at it with their phone or with their laptop.)

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘If statement with geo ip’ is closed to new replies.