Change Product Price based on Popularity in WooCommerce

    Praveen Chauhan
Listen to this article

Price Settings WooCommercePlugins are usually created for regular use cases. Which makes sense. Even an advanced plugin like WooCommerce, with so many of it’s features, cannot satisfy every use case. This is where a custom extension or add-on comes in to use. Consider a use case where you want to dynamically set the price of the product, based on the popularity of a product. This post is not about the Dynamic Pricing extension, but I will be mentioning it.


Let’s continue. I’ll explain my use case in detail. Say, I want to set the price of the product, based on the number of units sold. Thus price of the product is not based on the number of units being purchased, it depends on how many units have been purchased.


Are there any existing WooCommerce extensions that allow for variable pricing?

The Dynamic Pricing extension for WooCommerce, allows you to set a different price, or offer a discount, based on the number of product units purchased. A feature to be mentioned here is that, it allows you to set the price based on user roles. So if you want special pricing only for a special group of people, you can use this extension and customize it. But this extension by itself is not what we want.


Creating your own Custom Extension

The WooCommerce plugin provides several hooks for easy customization. So, when you do not find what you are looking for, you can easily create a custom extension or an add-on for the WooCommerce plugin. Our extension will set a different price for the product based on the number of items sold. For example, say the base price is $20. Once 100 items are sold, the price will be $25.

WooCommerce tracks the sales per product in the custom meta field, total_sales. Using the value for this field, we will know the total number of items sold, per product. This value, can be used to set the price using woocommerce_get_price filter. What we can do is create a simple WordPress plugin, and add the following code.

[pre]add_filter(‘woocommerce_get_price’, get_custom_product_price, $val, 2);

function get_custom_product_price($val) {
global $product;

// get the total number of items sold
$units_sold = get_post_meta( $product->id, ‘total_sales’, true );

if($units_sold >= 100)
return ‘25’;
// return original price 20
return $product->price;

Okay, agreed that not every customization is simple. You could even extend this functionality to set prices based on product ratings. And to provide better usability for such a plugin, you will have to create an admin UI which allows users to set price ranges, rules, etc.


Praveen Chauhan

Praveen Chauhan

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads