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. Overall, this WooCommerce customization will enhance the visibility of specific categories by applying such a discount in WooCommerce.
Solution: Provide 50% Discount to a Specific Category of Products in WooCommerce
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>'; } }
This to the shop owners who are running or planning to run BOGO offers on their WooCommerce store…
BOGO deals are great for increasing your sales, but have you thought about which offers are bringing you more revenue and which offers are not performing that great?
Don’t just set a BOGO deal, track the revenue generated by your deals in real-time with the Flexi BOGO for WooCommerce plugin.
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.
The similar 50% off tag is also displayed on the individual product page as shown below.
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.
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 and with more targeted promotions.