How to Show Default Regular Price for Woocommerce/WordPress based on Sale Price?


Woocommerce is a powerful plugin/framework that makes it possible to set up a online-store using WordPress. You can edit the attributes for each goods but sometimes you’ve got so many items that it may take time to update a particular attribute for a item.

For example, if you forgot to put the regular price for items, it will not show a regular price in cross fonts such as 100.00 GBP. But it would be nice to put the regular price so that the customers feel the items are on sale (cheaper) and this surely increases the purchase rate.

You can write a complex SQL to update the meta data but it will be risky and may not be so flexible than the following method. Put the following PHP code in your child-theme’s template functions.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function helloacm_regular_price( $price, $product ) {
  $price_factor = 1.3; // show sale price * 1.3 
  $sale_price = $product->get_sale_price(); // get sale price
  if (!is_numeric($sale_price)) { // sale price not set
    return $price;  // return default price.
  }
  $exp_price = $sale_price * $price_factor;  // get expected regular price
  if ($price > 0) {  // if regular price is filled
    if ($price < $sale_price) { // if it is even smaller than sale price
      return $exp_price;  // return corrected regular price
    }     
    return $price; // otherwise return the input regular price
  }
  return $exp_price; // return the default regular price
}
add_filter( 'woocommerce_get_regular_price', 'helloacm_regular_price', 10, 2);
function helloacm_regular_price( $price, $product ) {
  $price_factor = 1.3; // show sale price * 1.3 
  $sale_price = $product->get_sale_price(); // get sale price
  if (!is_numeric($sale_price)) { // sale price not set
    return $price;  // return default price.
  }
  $exp_price = $sale_price * $price_factor;  // get expected regular price
  if ($price > 0) {  // if regular price is filled
    if ($price < $sale_price) { // if it is even smaller than sale price
      return $exp_price;  // return corrected regular price
    }     
    return $price; // otherwise return the input regular price
  }
  return $exp_price; // return the default regular price
}
add_filter( 'woocommerce_get_regular_price', 'helloacm_regular_price', 10, 2);

That is it. If the regular price is not entered, it will show the default regular price which is $price_factor times the sale price. Otherwise, there will be a safety check to ensure the regular price is always more than the sale price.

This doesn’t change the database so it is considered the most safe way to play the tricks. The filter woocommerce_get_regular_price is added before returning the regular price.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
376 words
Last Post: C++ Simple Command Line Parameters Reader
Next Post: How to Add a Clear-Cart Button in Woocommerce?

The Permanent URL is: How to Show Default Regular Price for Woocommerce/WordPress based on Sale Price?

Leave a Reply