Setup WooCommerce Category Discount

How to Setup WooCommerce Category Discount (Easy Guide)

Shoppers in America alone went on a spending spree during the last holiday season, splashing nearly a trillion dollars. Well, that’s no surprise because holidays make up a significant portion of annual retail sales.

One of the major players in holiday online sale conversions is coupons and discounts, with 65% of shoppers citing them as key factors in their holiday purchase decisions.

You know what can have a bigger impact on your discount campaigns? Category-based discounts.

Categories in your WooCommerce store not only simplifies your inventory management, improve SEO and user experience, but with effective discount campaigns, they also allow you to drive more sales, boost seasonal/holiday sales, clear stock, and sell slow-moving products.

📌 In this article, we will explain how to create a WooCommerce category discount using different methods. Let’s get started.

How to Create WooCommerce Category Discount (3 ways)

There are several ways available for creating WooCommerce category discounts. We will demonstrate setting up WooCommerce discounts based on category in 3 ways –

  1. Category Discount Using Custom Codes
  2. Category Discount Using Coupons
  3. WooCommerce Category Discount Setup Using a Plugin

Let’s start with custom codes.

Way 1: WooCommerce Add Discount to a Category Using Custom Codes

You can auto-apply coupon or discount codes to your WooCommerce store by placing custom codes in your theme’s functions.php file. Let us walk you through the process.

Accessing the Theme File

Log in to your WordPress admin panel and go to Appearance >> Theme File Editor.

edit theme file

From the right panel, click the functions.php (Theme Functions) file.

edit funcitons.php

Scroll down to the bottom, create some empty space, and paste your code.

💡 Important note: You must back up your files or use a child theme before you attempt to edit theme files.

Custom Code for WooCommerce Category Discount

Let’s say you have a clothing store and want to offer a 10% discount on the category – Hoodie. The first thing you will need is the category ID.

The quickest way to get it is from the Categories page. Go to Products >> Categories and hover your mouse over the category to which you want to apply the discount.

collect category id

You will notice the browser will display a link at the bottom left, and from there, you can find the category ID.

Here’s the custom code for auto-applying a 10% discount on the Hoodie category.

add_action('woocommerce_before_calculate_totals', 'apply_category_discount');

function apply_category_discount($cart) {

if (is_admin() && !defined('DOING_AJAX')) return;

// Define the category ID for which you want to apply the discount

$category_id = 24; // Replace with your category ID

// Define the discount percentage

    $discount_percentage = 10;

// Loop through the cart items

foreach ($cart->get_cart() as $cart_item_key => $cart_item) {

     $product = $cart_item['data'];

     $categories = wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'ids'));

     // Check if the product is in the specified category

     if (in_array($category_id, $categories)) {

         // Calculate the discount amount

         $original_price = $product->get_price();

         $discount = ($original_price * $discount_percentage) / 100;

         $new_price = $original_price - $discount;

         // Set the discounted price

         $product->set_price($new_price);

     }

}

}

💡 Note:

  • Ensure you replace the category ID with your category ID.
  • Customize the discount percentage as needed for your store’s requirements.

Let’s test out if our code is working or not. Go to your front end and add a product from the Hoodie category to the cart.

add products to cart

WooCommerce will automatically apply the 10% discount on the cart page, as can be seen below.

WooCommerce category discount
Code Explanation

Action Hook: This code uses the WooCommerce hook woocommerce_before_calculate_totals, which allows you to modify cart item prices before the total is calculated.

Callback Function: The callback function apply_category_discount is called when the hook is triggered.

Category ID and Discount Percentage: You specify the category ID ($category_id) and the discount percentage ($discount_percentage) you want to apply to products within that category.

Looping Through Cart Items (foreach): This part loops through each item in the cart. For each item, it gets the product data and the list of category IDs to which the product belongs.

Getting Product Categories: It retrieves the categories of the current product using wp_get_post_terms.

Checking Category Membership: It checks if the product belongs to the specified category.

Calculating and Applying Discount: If the product is in your targeted category, the code calculates the discounted price based on the defined discount percentage and updates the product’s price accordingly using set_price().

Adding a WooCommerce Display Discount Badge on Category Page

We successfully applied a 10% WooCommerce discount on category Hoodie. However, it’s only visible on the cart page.

This discount is of no good to you unless you show your visiting customers that they can avail of a 10% discount on the products of this category. The purpose of offering discounts is to provoke your visitors to buy your products.

Therefore, you must promote your discount offerings everywhere possible, including on targeted categories and product pages. I am sure you can design or install promotional boxes such as pop-ups, banners, etc.

Anyway, let me show you how to mention the discount using codes. We will display the discount information on the category page and product pages. First, we want to display the WooCommerce discount by category information besides category name and product price.

You’ll need to add custom hooks to both the product category archive pages and the single product pages to display a badge or message beside the product price and the category name. Below are the steps to achieve this:

Display the WooCommerce Category Discount Badge on Category Archive Pages beside Product Price

Add the following code to your functions.php file beneath the previous code we applied:

// Display discount badge beside product price on category archive pages

add_action('woocommerce_after_shop_loop_item_title', 'display_category_discount_badge_archive', 15);

function display_category_discount_badge_archive() {

global $product;

// Define the category ID for which you want to display the discount badge

$category_id = 24; // Replace with your category ID

// Check if the product belongs to the specified category

if (has_term($category_id, 'product_cat', $product->get_id())) {

     // Define the discount percentage

     $discount_percentage = 10; // Replace with your discount percentage

     // Display the discount badge

     echo '<span class="category-discount-badge">Category Discount: ' . $discount_percentage . '% off</span>';

}

}

Here’s what this code will do –

WooCommerce category discount
Add the Category Discount WooCommerce Badge Next to the Price on Single Product Pages

Paste the following code below the previous codes in funcitons.php.

paste code
// Display category discount badge next to product price on single product pages

add_action('woocommerce_single_product_summary', 'display_category_discount_badge_next_to_price', 11);

function display_category_discount_badge_next_to_price() {

global $product;

// Define the category ID for which you want to display the discount badge

$category_id = 24; // Replace with your category ID

// Define the discount percentage

$discount_percentage = 10; // Replace with your discount percentage

// Check if the product belongs to the specified category

if (has_term($category_id, 'product_cat', $product->get_id())) {

     // Display the discount badge

     echo '<span class="category-discount-badge"> Enjoy a ' . $discount_percentage . '% off</span>';

}

}

Make sure you replace the category ID with the appropriate one. Here’s how the front end displays the information.

WooCommerce category discount message
Display Discount by Category WooCommerce Message beside Category Name

The following code will display a WooCommerce category discount message beside the category heading.

// Display discount message beside category name on category pages

add_action('woocommerce_archive_description', 'display_discount_message_beside_category_name');

function display_discount_message_beside_category_name() {

if (is_product_category()) {

     global $wp_query;

     // Get the current category object

     $category = $wp_query->get_queried_object();

        // Define the category ID for which you want to display the discount message

     $category_id = 24; // Replace with your category ID

     // Define the discount percentage

     $discount_percentage = 10; // Replace with your discount percentage

     // Check if the current category matches the specified category

     if ($category && $category->term_id == $category_id) {

         // Display the discount message

         echo '<p class="category-discount-message">Get ' . $discount_percentage . '% off on all ' . $category->name . ' products</p>';

     }

}

}
WooCommerce category discount message
CSS Code to Stylize WooCommerce Discount per Category

Additionally, you can add CSS codes to your theme’s style.css file to stylize the message and badges.

For example –

.category-discount-badge {

background-color: #ff0000; /* Red background */

color: #ffffff; /* White text */

padding: 5px 10px; /* Padding */

border-radius: 5px; /* Rounded corners */

display: inline-block; /* Ensure the badge is inline */

margin-top: 5px; /* Add space above the badge */

margin-left: 10px; /* Add space to the left of the badge */

}

.category-discount-message {

font-size: 18px; /* Adjust font size as needed */

color: #ff0000; /* Red text */

margin-left: 10px; /* Add some space to the left of the message */

display: inline-block; /* Ensure the message is inline */

}
Displaying Original Price in Strike-Through and Discounted Price besides That

To display the original price with a strikethrough and the discounted price for products in a specific category, you’ll need to modify the way prices are displayed on both the shop and single product pages. Below is a step-by-step guide to achieve this:

Step 1: Apply the Discount

Remove all the previous codes from this article if you have applied and paste the following.

add_action('woocommerce_before_calculate_totals', 'apply_category_discount');

function apply_category_discount($cart) {

if (is_admin() && !defined('DOING_AJAX')) return;

// Define the category ID for which you want to apply the discount

$category_id = 24; // Replace with your category ID

// Define the discount percentage

$discount_percentage = 10;

// Loop through the cart items

foreach ($cart->get_cart() as $cart_item_key => $cart_item) {

     $product = $cart_item['data'];

     $categories = wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'ids'));

     // Check if the product is in the specified category

     if (in_array($category_id, $categories)) {

         // Calculate the discount amount based on the regular price

         $original_price = $product->get_regular_price();

         if (is_numeric($original_price)) {

             $discount = ($original_price * $discount_percentage) / 100;

             $new_price = $original_price - $discount;

             // Apply the discount only if it's higher than the sale price

             if ($product->is_on_sale()) {

                 $sale_price = $product->get_sale_price();

                 if ($new_price < $sale_price) {

                     $product->set_price($new_price);

                 } else {

                     $product->set_price($sale_price);

                 }

             } else {

                 $product->set_price($new_price);

             }

         }

     }

}

}
Step 2: Display the Original and Discounted Price

Paste the following code beneath the above code.

add_filter('woocommerce_get_price_html', 'custom_price_html', 100, 2);

function custom_price_html($price, $product) {

// Define the category ID for which you want to apply the discount

$category_id = 24; // Replace with your category ID

// Define the discount percentage

$discount_percentage = 10;

// Check if the product is in the specified category

if (has_term($category_id, 'product_cat', $product->get_id())) {

     $regular_price = $product->get_regular_price();

     if (is_numeric($regular_price)) {

         $discount = ($regular_price * $discount_percentage) / 100;

         $discounted_price = $regular_price - $discount;

         if ($product->is_on_sale()) {

             $sale_price = $product->get_sale_price();

             $display_price = min($sale_price, $discounted_price);

         } else {

             $display_price = $discounted_price;

         }

         // Display original and discounted prices

         $price = '<del>' . wc_price($regular_price) . '</del> <ins>' . wc_price($display_price) . '</ins>';

     }

}

return $price;

}

This will display prices like this –

WooCommerce category discount
Step 3: CSS Styling

To ensure the prices are styled correctly, add the following CSS to your theme’s stylesheet (style.css):

/* Style for the original price with strikethrough */

.woocommerce div.product p.price del,

.woocommerce div.product span.price del {

color: #777;

text-decoration: line-through;

margin-right: 5px;

}

/* Style for the discounted price */

.woocommerce div.product p.price ins,

.woocommerce div.product span.price ins {

color: #FF0000; /* Red color for the discounted price */

font-weight: bold;

}
Displaying a Banner Image for WooCommerce Category Discount on Category & Product Pages
WooCommerce category discount banner

Banners and pop-ups are the most effective on-site marketing elements for displaying any sort of discount. Let’s check out how you can add a banner image to your category and product pages.

Step 1: Display a Discount Banner Image on the Category Page

Add the following code to functions.php –

add_action('woocommerce_archive_description', 'add_discount_banner_to_category_page', 5);

function add_discount_banner_to_category_page() {

if (is_product_category()) {

     // Define the category ID for which you want to display the discount banner

     $category_id = 24; // Replace with your category ID

     $discount_banner_url = 'https://example.com/path/to/your/discount-banner.jpg'; // Replace with your banner image URL

     $current_category = get_queried_object();

     if ($current_category->term_id == $category_id) {

         echo '<div class="discount-banner"><img src="' . esc_url($discount_banner_url) . '" alt="Discount Banner"></div>';

     }

}

}

Make sure you replace the image URL with an appropriate one.

replace URL

Here’s how the Category page displays the banner.

WooCommerce category discount banner

Way more attractive, right!

Step 2: Display a Discount Banner Image on Single Product Pages

Similarly, add these codes to funcitons.php to display product category discount WooCommerce banners on product pages.

add_action('woocommerce_single_product_summary', 'add_discount_banner_to_product_page', 5);

function add_discount_banner_to_product_page() {

// Define the category ID for which you want to display the discount banner

$category_id = 24; // Replace with your category ID

$discount_banner_url = 'https://example.com/path/to/your/discount-banner.jpg'; // Replace with your banner image URL

global $post;

    $terms = wp_get_post_terms($post->ID, 'product_cat');

$categories = wp_list_pluck($terms, 'term_id');

if (in_array($category_id, $categories)) {

     echo '<div class="discount-banner"><img src="' . esc_url($discount_banner_url) . '" alt="Discount Banner"></div>';

}

}

Again, replace the image URL. Here’s how it looks on the front end.

WooCommerce category discount banner product page

Way 2: WooCommerce Category Discount Using Coupon Codes

You must already be familiar with WooCommerce’s built-in coupon tool. You can also use coupons to create WooCommerce discount based on category.

Go to Marketing >> Coupons and hit the Add Coupon button.

add coupon

Enter your coupon name, select the discount type, and input the percentage value.

coupon details

Click on the Usage Restriction tab. From this window, you can filter your discount by products or categories.

user restriction

Click and select your category to set up WooCommerce discounts by category coupon.

assign category

Set usage limits and finally publish the coupon.

usage limits

Now, add any product under the Hoodie category and jump to the cart page. To apply the coupon, you need to click the Add a Coupon link.

add coupon

Insert the coupon code in the box and hit apply.

insert coupon code

It will apply a 10% off on your cart subtotal.

WooCommerce category discount coupon

Way 3: WooCommerce Category Discounts Setup Using a Plugin

Using custom code is pretty risky and not recommended for users who are not familiar with codes. The coupon options WooCommerce offers are also limited.

Overall, the built-in features don’t allow you flexibility in creating advanced and personalized discounts such as conditional discounts, bundle discounts, BOGO, etc. What if you want to offer a WooCommerce category discount based on the quantity added to the cart?

What if you only want to reward your loyal customers with the WooCommerce bulk discount category?

You can achieve these with a dedicated discount plugin. We will use the plugin called Discount Rules for WooCommerce.

WooCommerce category discount plugin

This plugin allows you to set up various automatic discounts and coupons. Simply put, you can create any type of discount you can think of using the plugin.

We have already demonstrated how the plugin works in our previous articles.

Here we will create the following discounts using the plugin.

  1. WooCommerce category discount based on quantity added
  2. Buy one, Get One discount based on category
  3. WooCommerce bulk discount based on category
  4. Discount based on category combination
  5. WooCommerce category discount based on cart total
  6. Discount based on user role

1. WooCommerce category discount based on quantity added

Go to WooCommerce >> Discount Rules and add a new rule.

add WooCommerce category discount rule

Select Cart Adjustment from the discount type dropdown. From the Filter tab, select Categories.

select category

Select the category to which you want to apply a WooCommerce category discount.

category selection

Let’s say we want to offer a 10% discount on Hoodie category products. Configure the settings from the Discount tab.

WooCommerce category discount

We also want the discount to apply when the customers add a minimum of 3 items to the cart from the Hoodie category. To do so, go to the Rules tab and select Item Quantity from the condition dropdown.

item quantity

Input other settings as follows.

item quantity set

Save this rule and jump to the front end of the site. Add 3 items from the Hoodie category and jump to the cart page.

WooCommerce category discount

As you can see, the WooCommerce category discount is applied based on the set quantity.

2. Buy one, Get One WooCommerce Category Discount

The plugin has a dedicated discount type for BOGO deals.

BOGO select

Select the category and from the Filters tab, assign your BOGO settings.

set discount

The above settings mean that customers will get one product free if they buy one product from the category Hoodie.

buy one get one

You can add and apply different discount ranges if necessary. For example, buy one and get a 10% discount on another; buy three, get one free, or buy seven to ten and get the last two at a fixed rate of $30.

BOGO deals

Let’s take another look at the front end with 7 products in the cart.

7 products

3. WooCommerce bulk discount based on category

It should be pretty clear and simple to you by now how to create bulk discounts using the plugin. There’s also a dedicated discount type for bulk discount.

bulk discount

After you select the discount type and category, go to the Discount tab.

WooCommerce category discount

As you can see, you can create WooCommerce discounts by category based on quantity using this method as well instead of using our previous item quantity condition method.

You can add different WooCommerce category discount ranges to meet your needs.

discount ranges

Here’s a look from the front end.

10 products

4. Discount based on Category Combination

Offering discounts for specific category combinations allows you to create special deals encouraging customers to buy products from different categories in a single purchase.

These tailored discounts benefit both shoppers and online store owners. Customers enjoy savings on various items, while store owners can increase sales across multiple categories.

For example, purchase from the Hoodie and T-Shirts category to enjoy a 15% discount. To set up this discount, select the Product Adjustment discount type and select the categories from the Filter tab.

product adjustment WooCommerce category discount

Set your WooCommerce category discount value.

WooCommerce category discount percentage

From the Rules tab, add the Category Combination condition.

category combination

Select the categories and configure other settings along with them.

WooCommerce category discount combination

Now, if you add products from these categories, WooCommerce will deduct 15% off them.

WooCommerce category discount

5. WooCommerce Category Discount Based On Cart Total

Let’s say you want to offer a 10% WooCommerce category discount only when the cart subtotal is $100 or more. Here’s how you set it in the plugin.

WooCommerce category discount based on subtotal

6. WooCommerce Category Discount Based On User Role

This type of discount is useful when you want to reward your loyal customers. There are multiple ways you can set up loyalty discounts using the plugin.

You can assign discounts based on your customer’s purchase history.

purchase history

Or you can specifically offer discounts to your valued members based on their user roles. This is particularly useful for a WooCommerce membership website.

user role WooCommerce category discount

Why Do You Need To Create a WooCommerce Category Discount?

In the competitive world of online stores, attracting customers and driving sales requires strategic marketing tactics. WooCommerce category discount offers a powerful tool to achieve just that.

Here’s a deep dive into why you should consider incorporating them into your WooCommerce store:

Boost Sales and Revenue

  • Incentivized Purchases: Discounts are a universally recognized motivator. By offering category discounts, you entice customers to purchase products they might have been considering but are hesitant about. The potential savings can push them over the edge and lead to increased sales.
  • Increased Average Order Value (AOV): Category discounts often encourage customers to buy more than just one discounted item. They might add complementary products from the same category to take full advantage of the discount. This will eventually lead to a higher overall purchase value. For instance, a discount on phone cases might incentivize them to add a screen protector as well.
  • Strategic Inventory Clearance: Do you have a surplus of products in a specific category that aren’t selling as quickly as others? Category discounts can be your secret weapon! Offering a WooCommerce category discount on these items can clear out slow-moving inventory and free up space for new products. This will ultimately generate revenue from stock that might otherwise sit idle.

Promotional Flexibility and Strategic Marketing

  • Targeted Product Highlighting: Let’s say you’ve launched a new clothing line for a specific season. Category discounts allow you to strategically target this new collection by offering a discount on the entire seasonal category. This draws attention to the new products and encourages customers to explore them.
  • Seasonal Promotions and Themed Sales: Category discounts are perfect for crafting seasonal promotions or themed sales. Imagine offering a discount on all your summer clothing categories during a “Summer Sizzle Sale.” This approach creates a sense of urgency and excitement around your seasonal offerings.
  • Highlight Specific Categories: Perhaps you have a category of high-quality products with a slightly higher price point. A targeted category discount can help showcase these items and encourage customers to consider the value proposition they offer.

Enhanced Customer Experience and Loyalty

  • Rewarding Loyal Customers: Category discounts can be a way to show appreciation to your regular customers. Offering exclusive discounts on their favorite categories can encourage repeat purchases and build customer loyalty.
  • Value Perception and Increased Satisfaction: A well-structured category discount can create a perception of greater value for customers. When they see a discount applied to a broader category purchase (e.g., a complete outfit), it can enhance their satisfaction with the deal.
  • Discovery and Exploration: Let’s say you offer a discount on a combination of categories like tools and hardware. This might introduce customers to new product lines they might not have considered before. This exploration could lead them to discover hidden gems in your store and become loyal customers for a wider range of items.

Wrap up

Incorporating category discounts into your WooCommerce marketing strategy offers a multitude of benefits. Hope this article will guide you in successfully creating a WooCommerce category discount using any of the options we mentioned above.

Leave a Reply

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


This site uses Akismet to reduce spam. Learn how your comment data is processed.

5,620,030+ Downloads. 589+ plus 5-star ratings. Promote products on any platform you want.