How to change Woocommerce price?

The prices of products on my site are collected through a robot. All prices are in Turkish lira. I want the price of all products to be converted into dollars. I used this code snippet.

function regular_price_func( $price, $product )
{
    //your logic for calculating the new price here
     $price = $product->get_regular_price()*1.3;

    //Return the new price (this is the price that will be used everywhere in the store)
    return $price;
}
add_filter('woocommerce_product_get_price', 'regular_price_func', 99, 2);

But when the special selling price is active for the product, the prices are wrong.

New codes:

// Change/Update Regular price 
 add_filter( 'woocommerce_product_get_regular_price', 'regular_price_func', 10, 2 );
 add_filter( 'woocommerce_product_variation_get_regular_price', 'regular_price_func', 10, 2 );
 function regular_price_func( $regular_price, $product ) {
      $rate = 1.3;
      if( empty($regular_price) || $regular_price == 0 ){
        return $product->get_price() * $rate;  
      } else{
        return $regular_price;  
      }
      
 }

 // You can also Change/Update Sale price 
 add_filter( 'woocommerce_product_get_sale_price', 'woo_sale_price_price_func', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'woo_sale_price_price_func', 10, 2 );
 function woo_sale_price_price_func( $sale_price, $product ) {
     $rate = 1.3;
     if( empty($sale_price) || $sale_price == 0 )
      return $product->get_regular_price() * $rate;
      else
     return $sale_price * $rate;
 };

Solutions

If you're looking to alter either the regular price OR the sale price. You have to specify which ones you want to use.

Also, the hook woocommerce_product_get_price is deprecated as of 3.0, the replacement for this is woocommerce_get_price - and finally, this filter returns price as a string, so to multiply a value, you need the floatval of that string.

function regular_price_func($price, $product) {
    if ($product->is_on_sale()){
        $price = floatval($product->get_sale_price()) * 1.3;
    } else {
        $price = floatval( $product->get_regular_price() ) * 1.3;
    }
    //Return the new price (this is the price that will be used everywhere in the store)
    return $price;
}

add_filter( 'woocommerce_get_price', 'regular_price_func', 99, 2 );

Try this, You can change the sale price and regular price and update the condition accordingly.

//Change/Update Regular price 
add_filter( 'woocommerce_product_get_regular_price', 'regular_price_func', 10, 2 );
add_filter( 'woocommerce_product_get_price', 'regular_price_func', 10, 2 );
add_filter( 'woocommerce_product_variation_get_regular_price', 'regular_price_func', 10, 2 );
function regular_price_func( $regular_price, $product ) {
    $rate = 1.3;
    if( empty($regular_price) || $regular_price == 0 ){
    return $product->get_price() * $rate;  
    } else{
    return $regular_price * $rate;  
    }
    
}

// You can also Change/Update Sale price 
add_filter( 'woocommerce_product_get_sale_price', 'woo_sale_price_price_func', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'woo_sale_price_price_func', 10, 2 );
function woo_sale_price_price_func( $sale_price, $product ) {
    $rate = 1.3;
    if( empty($sale_price) || $sale_price == 0 )
    return $product->get_regular_price() * $rate;
    else
    return $sale_price * $rate;
};

Similar questions

add seperate suffix for low price and high price in woocommerce
just trying to figure out how to add a suffix to prices that are lets say: $10 - $100 on the shop page to something like: $10 (a) - $100 (b) the only thing i found makes it $10 - $100 (a) - (b) but it displays this on each price variation and i don't want this to happen.
How to sort products by price ASC and place all no price products to the end? (Woocommerce)
I have such trouble, tried a lot of solutions, but nothing worked for me. I need to show all 'no price' products in the end of catalog, when I am sorting products by price ASC in Woocommerce.
WooCommerce: How do you add text before the price and before sale price?
I have managed to successfully get text to show up before the price and before the sale price, but the text is being considered part of the price not separate from it. Placing: in my custom CSS file successfully puts "Betty's price: " before the new "sale" price. The problem is that Betty's Price: is now underlined and clickable and its part of the...
How to write selling price on the next line of Regular price in wordpress woocommerce website?
I have uploaded some products in my portal. While uploading the products I have mentioned Regular price and then Selling Price. The prices are shown on the same row/line. If the price are longer in length, it is overflowing in the next line. I want to display regular price and selling price on the separate lines. Your Help will be appreciable. Than...
get minimum price variation and maximum price variation according to specific category woocommerce
I want to get minimum price and maximum price based on categories. The woocommerce query which gives me minimum and maximum product price range but i want it on the bases of category. for example: category="music,clothing". Here is query for minimum and maximum price: Please suggest me how can i get it according to category selected.
Woocommerce set sale price to regular price - 15% discount
I have a lot of products (nearly 30,000) in woocommerce. The regular price has been set properly, now I want to set the sale price to the regular one with some percentage of discount. I tried with some plugin but I had always a PHP memory error due to the numbers of products. So I'm trying to solve the problem directly in MYSQL. Here is my SQL code...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.