How to Apply a Coupon Code Automatically in WooCommerce/WordPress

If you’ve ever needed to automatically apply a coupon code at checkout for your WooCommerce customers, you may have noticed that there are a few plugins that can do this for you. However, with a little extra work, it’s easy to do yourself. Previous knowledge of the PHP and the WooCommerce folder structure is required.

Step 1. Set up your coupon code in WooCommerce. We probably don’t need to spend much time on that part.

Step 2. Open your functions.php file or your cart.php file.

Step 3. Add this code. Replace ‘nameOfCoupon’ with the name of your coupon.

Code

That’s all there is to it. One less plugin to maintain and it only took a few lines of code.