Automatic Coupons Based on Cart Total – 2024

In WooCommerce, applying coupons automatically can enhance the shopping experience by providing immediate rewards to customers as they meet certain spending thresholds. This tutorial will demonstrate how to implement “Automatic Coupons Based on Cart Total” using a custom function in your WordPress theme. By the end of this guide, you’ll be able to add a special discount automatically when the customer’s cart total reaches a specified amount.

Prerequisites

Before proceeding, ensure you have administrative access to your WordPress site and are comfortable editing PHP files. It’s recommended to perform these changes in a child theme to avoid losing customizations when updating the parent theme.

Step 1: Define the Coupon Details

First, you need to create a coupon in WooCommerce:

  • Go to WooCommerce > Coupons from your WordPress dashboard.
  • Click Add Coupon.
  • Define a coupon code, for example, 20OFF.
  • Set the discount type to a fixed cart discount or a percentage discount as desired.
  • Configure any additional restrictions or limits on the coupon usage.
  • Save the coupon.

Step 2: Add Custom Code to Your Theme’s functions.php File

Next, add the following PHP code to your theme’s functions.php file. This code checks the cart total and applies the “Automatic Coupons Based on Cart Total” when the total exceeds a specified amount.

/*
 * Snippet: Automatic Coupons Based on Cart Total – 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?959
* Tested with WooCommerce 8.8.2
*/ add_action('woocommerce_before_cart', 'wcsuccess_apply_automatic_coupon'); add_action('woocommerce_before_checkout_form', 'wcsuccess_apply_automatic_coupon'); function wcsuccess_apply_automatic_coupon() { $minimum_amount = 100; // Set minimum cart total required to apply the coupon $coupon_code = '20OFF'; // The coupon code you want to apply if ( WC()->cart->subtotal >= $minimum_amount ) { if ( ! WC()->cart->has_discount( $coupon_code ) ) { WC()->cart->add_discount( $coupon_code ); wc_print_notices(); } } else { if ( WC()->cart->has_discount( $coupon_code ) ) { WC()->cart->remove_coupon( $coupon_code ); wc_add_notice('The automatic discount has been removed as your total has dropped below ' . wc_price($minimum_amount), 'notice'); } } }

Explanation of the Code

  • Hooks: The wcsuccess_apply_automatic_coupon function is hooked into woocommerce_before_cart and woocommerce_before_checkout_form to check the cart’s total each time the cart or checkout page is loaded.
  • Cart Total Check: It checks if the cart subtotal (before taxes and shipping) is at least $minimum_amount (you can adjust this value as needed).
  • Apply Coupon: If the cart meets the minimum amount and the coupon hasn’t been applied yet, it applies the coupon.
  • Remove Coupon: If the cart total falls below the minimum required after the coupon has been applied, it removes the coupon and informs the user.

Testing Your Changes

  • Clear your WooCommerce cache and browser cache.
  • Add products to your cart and observe how the “Automatic Coupons Based on Cart Total” is applied or removed based on the total amount.
  • Adjust the $minimum_amount and $coupon_code as necessary to fit your specific campaign.

Conclusion

This tutorial provides a straightforward method to enhance customer engagement by applying “Automatic Coupons Based on Cart Total” in WooCommerce. It automatically rewards customers when they reach a certain spending threshold, fostering increased order values and customer satisfaction.

By integrating this functionality, you can offer a seamless discount experience, encouraging more purchases and boosting overall sales. Always test changes on a staging site before applying them to your live site to ensure compatibility with your specific WooCommerce setup.

0 0 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

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