WooCommerce Discount Percentage allows store owners to grant a discount, based on what the customer purchases. It could be a discount on a product or the customer’s entire cart. When used wisely, the benefits can go from increasing the average customer order to getting rid of unsold inventory. The customers know the higher their purchase is, the higher the discount will be. It also gives a nudge to your target audience to make their first purchase, therefore increasing your clientele.
There are 3 methods to offer WooCommerce Discount Percentage in your store:
- Using coupons
- Using a Woocommerce discount plugin
- Using the programming language – PHP: This is majorly for Web developers but you can also try it out
Let’s dive in!
Table of Contents
How to offer WooCommerce Discount Percentage using coupons?
We already have a well-curated article about how to create a WooCommerce percentage discount using coupons. Kindly visit here to check it out.
How to offer WooCommerce Discount Percentage using a Discount Plugin?
There are lots of discount plugins, but very few are simple to use and efficient. In this tutorial, we will be using the ‘Conditional Discounts for WooCommerce’ plugin. This is a Discount plugin that is impeccable for all types of discounts; percentage discounts, BOGO, fixed amounts discounts, bulk discounts, wholesales discounts, etc. It automatically applies discounts to customers that meet the criteria defined by the store owner. It has a free version that is perfect for what we are about to do!
Steps to Follow;
-
Offering percentage discount On specific products
- The very first step is to download and install the free version of the plugin here.
- Then let’s create a woocommerce product list that will contain all the products to which the discount will be applied to.
- Once it’s done, kindly go to Discounts > New discount to access the discounts creation page:
- Please enter a discount name
- Select “Percentage off product price” in the Action field
- Enter your discount percentage in the “Percentage / Fixed amount” field
- Click on the Publish button to push your discount live.
- If you want to add conditions to the application of the discount, kindly click on the “Add rules group’ button and set your terms according to your needs.
-
Offering percentage discount On cart total
Simply follow the previous steps but select “Percentage off order subtotal” instead of “Percentage off product price” in the “Action” field to apply a percentage discount off the order. That way, instead of applying the discount on a specific set of products, it will be applied to the entire cart.
How to offer a Woocommerce Discount Percentage using PHP?
PHP is the programming language that both Woocommerce and WordPress are built on. In this section, we will be explaining how you can set up a percentage discount by writing a piece of code. I will strongly advise that if you do not have coding experience, you might want to ask for assistance.
Before you set up a percentage discount on a woocommerce product using a coding language, you need to understand the actions, filters and hooks. Actions and filters are two different types of hooks that allow you to run PHP custom codes at a specific point in the execution of WordPress Core, plugins, and themes.
WooCommerce as a WordPress plugin includes many hooks which allow you to alter its default behavior from your own plugin or your theme.
-
Offering percentage discount On specific products
We have two types of products; we have simple products and variable products. Simple products refer to products with no variations or options while variable products have different options and variations, which could differ in prices. A good example of a variable product is a T-shirt, it is available in different sizes and colors.
For offering percentage discounts on simple products, we’ll use two filters:
- woocommerce_product_get_price and
- woocommerce_product_get_sale_price.
add_filter( 'woocommerce_product_get_price', 'get_product_sale_price', 10, 2 ); add_filter( 'woocommerce_product_get_sale_price', 'get_product_sale_price', 10, 2 ); function get_product_sale_price( $sale_price, $product) { //This function will go through every product displayed on the page when the code runs //so we need to make sure to apply the discount on the product we need which ID is $product_to_discount_id $product_to_discount_id=123; $percentage=20; //We retrieve ID of the product that is being evaluated $product_id = $product->get_id(); //If it's the product we want to discount if ( $product_id == $product_to_discount_id ) { $percentenge_amount = $percentage/100; $sale_price = (float) $sale_price; //we apply the discount percentage and calculate the new price $sale_price = $sale_price - ( $sale_price * $percentenge_amount ); } return $sale_price; }
For variable products, we’ll use two other filters different from the ones used for simple products:
- woocommerce_product_variation_get_sale_price and
- woocommerce_product_variation_get_price.
add_filter( 'woocommerce_product_variation_get_sale_price', 'get_product_sale_price', 10, 2 ); add_filter( 'woocommerce_product_variation_get_price', 'get_product_sale_price', 10, 2 ); function get_product_sale_price( $sale_price, $product) { //This function will go through every product displayed on the page when the code runs //so we need to make sure to apply the discount on the product we need which ID is $product_to_discount_id $product_to_discount_id=123; $percentage=20; //We retrieve ID of the product that is being evaluated $product_id = $product->get_id(); //If it's the product we want to discount if ( $product_id == $product_to_discount_id ) { $percentenge_amount = $percentage/100; $sale_price = (float) $sale_price; //we apply the discount percentage and calculate the new price $sale_price = $sale_price - ( $sale_price * $percentenge_amount ); } return $sale_price; }
-
Offering percentage discount On cart total
Just like earlier, we’ll use WooCommerce hooks to apply our percentage discounts on the woocommerce cart. If you jumped directly to this section, kindly learn about actions, filters, and hooks here, as it’s important to what we are about to do.
//We'll use the “woocommerce_cart_calculate_fees” action to create our discount add_action( 'woocommerce_cart_calculate_fees', 'add_order_discount' ); function add_order_discount() { $percentage = 20; $discount_amount = (float) $percentage/100; $order_subtotal = WC()->cart->get_subtotal(); $fee = array( 'name' => __( 'Discount on cart' ), 'amount' => (float) - 1 * $order_subtotal * $discount_amount, ); wc()->cart->fees_api()->add_fee( $fee ); }
And it’s a wrap! All the 3 ways to offer WooCommerce Discount Percentage, I hope this tutorial helps you out.
Kindly share your thoughts, experiences, or questions in the comment section below.