In this article, we’ll show you how to apply coupons only to regular products, ensuring that sale items are not eligible for additional discounts.
Step 1: Disabling Coupons for Sale Products To disable coupons for products that are on sale, you can add custom code to your theme’s functions.php
file. Here’s the code you need:
function disable_coupon_for_sale_products( $is_valid, $coupon ) { // Check if the coupon is applicable to sale items if ( $coupon->is_type( 'fixed_product' ) || $coupon->is_type( 'percent_product' ) ) { // Get the products in the cart $cart = WC()->cart->get_cart(); foreach ( $cart as $cart_item_key => $cart_item ) { // Check if the product is on sale if ( $cart_item['data']->is_on_sale() ) { $is_valid = false; // Disable the coupon for sale products break; } } } return $is_valid; } add_filter( 'woocommerce_coupon_is_valid', 'disable_coupon_for_sale_products', 10, 2 );
Step 2: Excluding Sale Products from Coupons Additionally, you can exclude sale products from coupons altogether. This ensures that the coupon won’t be applicable to any product marked as “on sale.” Use the following code to achieve this:
function exclude_sale_products_from_coupon( $excluded_product_ids ) { // Get the products in the cart $cart = WC()->cart->get_cart(); foreach ( $cart as $cart_item_key => $cart_item ) { // Check if the product is on sale if ( $cart_item['data']->is_on_sale() ) { $excluded_product_ids[] = $cart_item['product_id']; // Exclude sale products from coupon } } return $excluded_product_ids; } add_filter( 'woocommerce_coupon_get_excluded_product_ids', 'exclude_sale_products_from_coupon' );
By following the steps outlined in this article, you can easily apply coupons only to regular products in your WooCommerce store.
Make sure to create a backup of your website and use a child theme or custom plugin for adding custom code.