skip to Main Content

I´m running a WooCommerce store (WordPress 5.4.1 and WooCommerce 4.1.0), and I´m trying to set prices based on customer location, so for this I usually a small snippet, I have been updating deprecated functions of my snippets, I have updated a few ones with any kind of issue but with this one, I don´t understand why the deprecated method works, but the new ones not.

Here is the code with the deprecated version instead of woocoomemerce_product_get_price the old method uses woocommerce_get_price, it works, the price for the customers of Barcelona is increased by the right multiplier, but this code affects WordPress, per example if I try to update a plugin I have an error, or if I try to search a new plugin, any result is shown until I press f5.

add_filter( 'woocommerce_get_price', 'change_specific_products_price', 10, 2 );
function change_specific_products_price( $price, $product ) {
$userInfo = geoip_detect2_get_info_from_current_ip();

if ( has_term('skf', 'product_cat', $product->get_id() ) && $userInfo->city->name == 'Barcelona' ) {
   $price *= 1.20;
}
return $price;
} 

So I decided to update deprecated functions, I check WooCommerce documentation, but I don´t understand why the deprecated method works, but the new ones not.

Here is the updated version of the code

  add_filter( 'woocommerce_product_get_price', 'change_specific_products_price_2', 10, 2 );
  function change_specific_products_price_2( $price, $product ) {
  $userInfo = geoip_detect2_get_info_from_current_ip();

if ( has_term('skf', 'product_cat', $product->get_id() ) && $userInfo->city->name == 'Barcelona' ) {
   $price *= 1.20;
}
return $price;
} 

I´m stuck at this point, any help will be great.

2

Answers


  1. Chosen as BEST ANSWER

    Many thanks for your reply , but the code does not work , it´s something weird , with the deprecated function works , but with the new one not , that´s why i´m complete lost.

    I have contacted with the author´s theme , if this code can affect in some way how woocommerce works , or the theme .

    Is like this code breaks ajax reload , that´s the reason why when i search for a new plugin in the wordpress backend ,don´t show anything until i refresh manually the page by pressing f5.


  2. add_filter( 'woocommerce_product_get_price', 'change_specific_products_price', 10, 2 );
    
    function change_specific_products_price( $price, $product ) {
    
        $userInfo = geoip_detect2_get_info_from_current_ip();
    
        if ( has_term( 'cloth', 'product_cat', $product->get_id() ) && $userInfo->city->name == 'Barcelona' ) {
            $price *= 1.20;
        }
        return $price;
    }
    

    This works fine.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search