As a shop owner, it is important to give discounts and offers to the customers frequently. Mostly, it is for offering discounts by reducing the product prices, and later increasing when you are planning to stop the special price or offer in your WooCommerce store. This price changing task is not that much complicated in the WooCommerce store when it comes to one or two products. But it is really a tedious task to change the prices of a large number of products. So what is a quick solution?
Yes, when it comes to increasing or decreasing the price of a large number of products, you can do it programmatically. If you are not technically savvy and not that much proficient in coding, this article will help you by giving the code snippet to be used on your WooCommerce site to implement the discount for a number of products.
The code can be used to apply the discount for both simple and variable products. This will be useful, and one of the best ways to apply a price change to products in your store if you are not planning to opt for other solutions such as integrating an offer & discount plugin or extension.
Now, let us get into the code part.
Add Discount Programmatically on WooCommerce
As you know, WooCommerce and WordPress platforms usually use PHP. And the actions and functions usually work with PHP functions. Here also we are using a PHP hook.
Note: PHP or code hook is a specially defined part in the program code that can pass control to an add-on. A hook is declared by calling a special function in the necessary part of the code.
Check an example of a PHP hook below to apply the discount or price change to the products in your WooCommerce store for the user roles administrator and vendor.
add_action( 'woocommerce_cart_calculate_fees', 'elex_discount_price' ); function elex_discount_price() { global $woocommerce; //Set the price for user role. $discount_price = 5; $set_role = array('administrator','vendor'); //Get the login user role $user = wp_get_current_user(); $user_role = $user->roles; //Check the specifc user role for discount. if( in_array( $user_role[0] , $set_role ) ) { $woocommerce->cart->add_fee( 'Discounted Price', -$discount_price, true, 'standard' ); } }
According to the above code, $5 will be deducted from the user roles administrator and vendor. Similarly, you can customize the code according to your requirements.
Here the function is, taking the total amount from the cart and applying a discount if the user role is administrator or vendor.
This is one method to apply the discount to your store. If you want to apply the discount dynamically and need to apply different types of discounts to different products and its variations, it is better to opt for dynamic pricing and discounts plugin in your store. I would like to give you an idea to apply a discount with the help of a plugin.
Add Discount with a Plugin
As WooCommerce is flexible to integrate many features with the help of various plugins, it is easy to pick a plugin for making your task easy. And, if you do not want to spend too much time on coding, you can easily get a plugin to help you. If you are not planning to spend much on your store, you can choose a free version of the offers and discounts plugin. Later, if you need to upgrade your store with different varieties of offers, discounts, and other features, you can pick a robust premium plugin.
If you want to provide discounts in your store with a free plugin, I recommend ELEX WooCommerce Dynamic Pricing and Discounts Free Plugin. This plugin mainly comes up with 2 default discount rules such as Product Rules and Category Rules.
Product Rules
Product rules let you apply the discount on products individually based on the Quantity, Weight, or Price of the products added by the customer into their shopping cart. Additionally, you can apply the discount to all products in a single category or products in the shopping cart.
Category Rules
Category Rules let you apply the discount to the products which come under one or more product categories in your store, based on the Quantity, Weight, price, or total units of the products in customers’ shopping cart.
The advantage of using this plugin is, you can easily apply a percentage, flat, or fixed price discount in minimum clicks. You do not need to worry about complex codes and all. You can simply configure it like you are filling up a form.
For more clarity on configuring a discount on this plugin, check the below example which I have set with Product Rules.
As you can see, the interface of the plugin is very user friendly, as you can enter the details accurately, save the rules, and simply relax. Here in the above example, I have chosen an individual product Happy Ninja T-Shirt, and if a customer picks a minimum of 3 items of it, they will be eligible for a 10% discount.
Similarly, you can apply flat or fixed price discounts.
As you have seen the discount applied for user roles, you can simply select the user roles from a drop-down pickup list in this plugin on ‘Allowed Roles & Date’.
To know more about this plugin, its various tabs, and how to configure it, got through an article: How to Set Up ELEX Dynamic Pricing and Discounts Plugin for WooCommerce?
With this plugin, you can also display the pricing table on individual product pages to make the customers’ shopping experience more easy.
Or if you need to have more advanced discount rules such as combinational rules and Buy and Get Free Offer (BOGO) Rules, you can go for its premium version. The premium version plugin is highly rich in its features and comes up with 9 default discount rules as well as other features to filter the customers and make the discount available for a certain period of time. The discount rules available on its premium version are:
- Product Rules
- Category Rules
- Combinational Rules
- Category Combinational Rules
- Cart Rules
- Buy and Get Free Offer (BOGO) Rules
- Buy and Get Offer (BOGO) Category Rules
- Tag Rules
- BOGO Tag Rules
To know more about the plugin, you can go through an article: Everything You Need to Know About WooCommerce Dynamic Pricing.
Final Thoughts
You can go for the code snippet option if you need to apply the same price change or discount to all products in your store. Yes, it is convenient if you do not want to spend on integrating the dynamic pricing in your store and you are technically sound. But if you need a complete solution, which eases your work and provides a user-friendly platform to apply different types of discounts, you can choose ELEX WooCommerce Dynamic Pricing and Discounts Free Plugin or its premium version for more advanced dynamic pricing and discounts features.