It’s Christmas and with Christmas comes offers galore. After all, discounts are a great way to increase sales, right? As a WooCommerce store owner, you too might have surely given discounts to your customers.
But, wait a minute. Sometimes customers don’t apply the coupon code to avail of the discount and you are left wondering why? Well, there could be only one reason for this to happen and that is that the customer did not notice the coupon code.
Now if you’re that person who doesn’t want customers to miss out on the discounts you might want a way to be able to apply the discount coupon automatically based on the offer on your WooCommerce store and I’m going to provide you with a solution for just that!
The following are two ways to apply a discount coupon automatically to a customer’s purchase:
1. Using a Plugin like ‘WooCommerce Auto Added Coupons’
- WooCommerce Auto Added Coupons plugin allows users to create a coupon code in WooCommerce and then automatically apply it to the cart.
- This option is provided to the user in the coupons tabs of WooCommerce in the dashboard.
- The admin just needs to check a simple checkbox to apply the discount coupon automatically.
- Let us understand this with an example. Suppose a 25% discount coupon has been set on cart level for a minimum order total of $100. Now, when the total order amount in the customer’s cart fulfills the coupon condition a 25% discount is automatically applied to the customer’s total order amount.

- This plugin is useful if you want to apply a discount coupon automatically at cart level or at product level for a certain minimum or maximum amount spent by the customer.
2. Programmatically Apply Discount Coupon to Customer’s Purchase
- Let us assume a situation in which you might want to apply a discount coupon based on the collective weight of all the products in the cart or based on the address the product has to be shipped. In such a scenario the above plugin will not be useful.
- In such a scenario you can programmatically apply the discount coupon to the order.
- The first step in order to be able to do so would be to create a coupon in WooCommerce.
- WooCommerce provides an option to create a coupon from the dashboard. However for the inquisitive minds out there you can use the following code provided by WooThemes to create the coupon code manually.
$coupon_code = 'UNIQUECODE'; // Code $amount = '10'; // Amount $discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product $coupon = array( 'post_title' => $coupon_code, 'post_content' => '', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'shop_coupon' ); $new_coupon_id = wp_insert_post( $coupon ); // Add meta update_post_meta( $new_coupon_id, 'discount_type', $discount_type ); update_post_meta( $new_coupon_id, 'coupon_amount', $amount ); update_post_meta( $new_coupon_id, 'individual_use', 'no' ); update_post_meta( $new_coupon_id, 'product_ids', '' ); update_post_meta( $new_coupon_id, 'exclude_product_ids', '' ); update_post_meta( $new_coupon_id, 'usage_limit', '' ); update_post_meta( $new_coupon_id, 'expiry_date', '' ); update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' ); update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
- Once the coupon code has been created it can then be used to apply discount coupon automatically using the following code.
//The folloing is the code to apply discount coupon automatically when the quantity of products in cart is greater than 10 add_action('woocommerce_before_cart_table', 'discount_when_quantity_greater_than_10'); function discount_when_quantity_greater_than_10 { global $woocommerce; global $total_qty; if( $total_qty > 10 ) { $coupon_code = 'UNIQUECODE'; if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) { $woocommerce->show_messages(); } echo '<div class="woocommerce_message"><strong>The number of Product in your order is greater than 10 so a 10% Discount has been Applied!</strong></div>'; } }
Using the above code you can apply discount coupon automatically in various situations such as when the total weight of products in cart reaches a certain amount or when the shipping address is within a certain area. The applications are many but the code remains the same in all situations. Of course, you gotta put your own conditions! ;)
So, that was about how to apply discount coupon automatically. If you have anything you would like to ask or add please feel free to use the comments section. Until next time adios guys :)