Woocommerce How To Assign Discount Code To Discounted Products

WooCommerce: Mastering Discount Codes on Sale Items (The Beginner’s Guide)

So, you’re running a WooCommerce store and want to reward your customers with discount codes, especially on products already on sale? Great! That’s a smart way to boost sales and encourage purchases. But figuring out how to apply those codes *only* to products *already discounted* can be a bit tricky. Don’t worry, this guide breaks it down for newbies in an easy-to-understand way.

Why Discount Codes on Sale Items?

Think of it like this: You walk into your favorite clothing store and see a rack of items already marked 50% off. Suddenly, a store employee hands you a coupon for an *additional* 20% off *everything*, including sale items. You’d be thrilled, right? That’s the power of layering discounts.

Here’s why it’s beneficial:

    • Increased Perceived Value: Customers feel like they’re getting an unbelievable deal.
    • Encourages Purchases: The extra discount can push hesitant buyers over the edge.
    • Clears Inventory: Helps you move older or excess stock quickly.
    • Builds Customer Loyalty: Rewards your customers and makes them feel appreciated.

    The Challenge: Default WooCommerce Settings

    Out of the box, WooCommerce doesn’t *natively* allow you to easily apply a discount code exclusively to products that are *already* on sale. If you create a regular discount code, it usually applies to all eligible products, regardless of whether they’re on sale or not.

    So, how do we overcome this limitation? We have a few options:

    1. Using the “Products on sale” filter (Best for simple scenarios)

    2. Custom Code (For full control and advanced features)

    Let’s explore each option:

    Option 1: WooCommerce’s “Products on sale” Restriction

    This is the simplest method if you just want to provide the discount to products on sale.

    1. Go to WooCommerce > Coupons > Add Coupon.

    2. Fill in the Coupon Code, Description, and Discount Type (e.g., Percentage discount, Fixed cart discount).

    3. Scroll down to the Usage Restriction tab.

    4. Under the “Products” field, type the word “on sale” and select the “Products on sale” option in the dropdown.

    That’s it! This coupon will now only be applied to items that are on sale.

    Option 2: Custom Code for Advanced Control (The Programmer’s Approach)

    For those who want finer control and more complex scenarios (e.g., apply the discount only to certain categories *and* sale items), custom code is the way to go. This involves adding Explore this article on How To Customize Woocommerce Cart a small snippet of code to your theme’s `functions.php` file (or Read more about How To Use Visual Builder Woocommerce Divi a custom plugin). Important: Always back up your website before editing code!

    Here’s the code snippet:

     add_filter( 'woocommerce_coupon_is_valid_for_product', 'ts_coupon_valid_for_sale_products', 10, 4 ); 

    function ts_coupon_valid_for_sale_products( $valid, $product, $coupon, $values ) {

    // Set your coupon code here

    $coupon_code = ‘SALE20’; // Replace with your actual coupon code

    if ( $coupon->get_code() == $coupon_code ) {

    if ( ! $product->is_on_sale() ) {

    $valid = false;

    }

    }

    return $valid;

    }

    Explanation:

    • `add_filter( ‘woocommerce_coupon_is_valid_for_product’, ‘ts_coupon_valid_for_sale_products’, 10, 4 );` This line hooks into WooCommerce’s coupon validation process. It tells WooCommerce to run our custom function (`ts_coupon_valid_for_sale_products`) whenever a coupon is checked against a product.
    • `function ts_coupon_valid_for_sale_products( $valid, $product, $coupon, $values ) { … }` This is our custom function. It takes four arguments:
    • `$valid`: A boolean value (true or false) indicating whether the coupon is currently valid for the product.
    • `$product`: The WooCommerce product object being checked.
    • `$coupon`: The WooCommerce coupon object.
    • `$values`: An array of values related to the product and cart context.
    • `$coupon_code = ‘SALE20’;` Replace `’SALE20’` with your actual coupon code! This is the code the script looks for to apply the functionality.
    • `if ( $coupon->get_code() == $coupon_code ) { … }` This checks if the coupon code entered by the user matches the one we’ve specified. We only want this logic to apply to *our* special sale item coupon.
    • `if ( ! $product->is_on_sale() ) { $valid = false; }` This is the key line. It checks if the product is *not* on sale. If it’s not on sale, we set `$valid` to `false`, effectively making the coupon invalid for that product.
    • `return $valid;` Finally, we return the `$valid` value. WooCommerce uses this to determine whether the coupon should be applied.

    How to Implement:

    1. Backup your website. Seriously.

    2. Access your theme’s `functions.php` file: You can usually find it in your WordPress dashboard under Appearance > Theme Editor. Be VERY careful! Make sure you know what you are doing. An error in the code can make the site unresponsive!

    3. Paste the code: Add the code snippet to the *end* of your `functions.php` file, *before* the closing `?>` tag (if it exists).

    4. Customize the code: Remember to replace `’SALE20’` with your actual coupon code!

    5. Save the file.

    Important Considerations:

    * Child Theme: Ideally, you should implement custom code in a child theme. This prevents your changes from being overwritten when you update your theme.

    * Plugin: A more robust solution is to create a custom plugin for this functionality. This keeps your theme clean and makes the code more portable.

    * Testing: Always thoroughly test your code after implementing it to ensure it’s working as expected.

    Combining Sale Restrictions with other Coupon Restrictions

    The power of the code method (and to an extend the simpler method) comes from the possibility to add further restrictions. For example, you can set a minimum spend and make the coupon valid for specified user roles. In particular, the code method enables developers to further limit the coupon application using product categories, attributes, or tags. This is done by adding conditions to the `if` statement.

    Troubleshooting Common Issues

    • Coupon Not Applying: Double-check that the coupon code is entered correctly. Make sure the product is actually on sale. Verify that the code snippet is correctly implemented (no syntax errors).
    • Coupon Applying to All Products: Ensure you’ve correctly specified the coupon code in the code snippet. Re-check that the `if` statement correctly tests if the product is on sale.

Conclusion

By using the “Products on sale” WooCommerce filter, or custom code, you can easily reward your customers with additional discounts on already reduced items. It’s a powerful marketing technique that will surely bring value to your online store. Remember to properly test everything before making it live. Good luck!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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