Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Provide 50% Discount to a Specific Category of Products in WooCommerce?

It’s very common to implement a promotion during seasonal sales, holidays and also on event specific promotions too. For example, an electronic store might offer a 50% discount on Electronic Accessories (a specific category). This helps customers who are in search of electronic products to grab such offers targeted to a specific category of products.

This code offers a 50% discount which is displayed on shop loop pages and the discount gets applied on the cart page.

  // Display "50% off" tag on shop page for products in category
add_action('woocommerce_before_shop_loop_item_title', 'ts_display_discount_tag_shop_page', 10);
function ts_display_discount_tag_shop_page() {
    global $product;

    // Check if the product belongs to category
    if (has_term('electronic-accessories', 'product_cat', $product->get_id())) {
        echo '<div style="background-color:#4CAF50;; color: #fff; display: inline-block; padding: 5px 10px; border-radius: 5px;"><span style="font-size: 16px; font-weight: bold;">50% off</span></div>';
    }
}


// Apply 50% discount to products in category
add_action('woocommerce_before_calculate_totals', 'ts_apply_discount_category', 10);
function ts_apply_discount_category($cart) {
    if (is_admin() && !defined('DOING_AJAX')) return;

    // Category to apply discount
    $targeted_category = 'electronic-accessories';

    // Loop through cart items
    foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
        $product = $cart_item['data'];

        // Check if the product belongs to the targeted category
        if (has_term($targeted_category, 'product_cat', $product->get_id())) {
            // Calculate new price with 50% discount
            $new_price = $product->get_price() * 0.5; // 50% off

            // Set new price
            $cart_item['data']->set_price($new_price);
        }
    }
}

// Display "50% off" tag on individual product pages for products in category
add_action('woocommerce_before_single_product_summary', 'ts_display_discount_tag_single_product', 5);
function ts_display_discount_tag_single_product() {
    global $product;

    // Check if the product belongs to category
    if (has_term('electronic-accessories', 'product_cat', $product->get_id())) {
        echo '<div style="background-color: #4CAF50; color: #fff; display: inline-block; padding: 5px 10px; border-radius: 5px; margin-bottom: 15px;"><span style="font-size: 16px; font-weight: bold;">50% off</span></div>';
    }
}

Output

The code helps to display that the 50% off is available for the specific category ‘Electronic Accessories’ as specified in the code (referred to as category ID 63) on both the shop page and in the individual product page by using the respective hooks available for it.

How to Provide 50% Discount to a Specific Category of Products in WooCommerce? - Tyche Softwares

The similar 50% off tag is also displayed on the individual product page as shown below.

How to Provide 50% Discount to a Specific Category of Products in WooCommerce? - Tyche Softwares



The part of the code hooks into the woocommerce_before_calculate_totals action checks if the product belongs to the targeted category as specified in the code. If it does, it calculates a new price with a 50% discount and sets that as the new price for the product in the cart.

How to Provide 50% Discount to a Specific Category of Products in WooCommerce? - Tyche Softwares


Similarly, you can offer free products as gifts instead of offering discounts based on product categories. This guide comes up with multiple code snippets that will help you to automatically add a product to your WooCommerce cart based on different conditions.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x