Title: Programmatic Quantity Based Coupons
Last modified: October 23, 2019

---

# Programmatic Quantity Based Coupons

 *  Resolved [autifsnuc](https://wordpress.org/support/users/autifsnuc/)
 * (@autifsnuc)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/programmatic-quantity-based-coupons/)
 * I’m trying to implement code that will make it so that users can enter a coupon
   code which will only work once they’ve added a certain quantity or greater to
   their cart. The code that I have so far is based off of this tutorial:
 * [https://coding.mangopear.co.uk/resources/automatically-apply-woocommerce-coupons-based-on-cart-quantity/](https://coding.mangopear.co.uk/resources/automatically-apply-woocommerce-coupons-based-on-cart-quantity/)
 * Here is my code:
 *     ```
       <?php
   
       function is_individual_coupon_applied() {
           global 	$woocommerce; // [a]
   
           $coupon_args = array(
               'posts_per_page'	=> -1,
               'orderby'			=> 'title',
               'order'				=> 'asc',
               'post_type'			=> 'shop_coupon',
               'post_status'		=> 'publish',
               'meta_key'			=> 'individual_use',
               'meta_value'		=> 'yes',
           );
   
           $coupons = get_posts($coupon_args); // [b]
   
           foreach ($coupons as $coupon) : // [c]
               $coupon_title = get_the_title($coupon); // [d]
   
               if ($woocommerce->cart->has_discount($coupon_title)) : return true; // [e][f]
               else : return false; // [e][g]
               endif;
           endforeach;
       }
   
       function bulk_discount_coupons() {
           global 	$woocommerce; // [a]
   
           if (is_cart() || is_checkout())  {
               if (is_individual_coupon_applied() != true){
                   $coupon_five_percent = '5% discount'; // [b]
                   $coupon_ten_percent  = 'TOOMANYNUCS'; // [b]
                   $cart_contents_count = $woocommerce->cart->cart_contents_count; // Get cart contents
   
                   if ($woocommerce->cart->has_discount($coupon_ten_percent)) {
                       $woocommerce->cart->remove_coupon($coupon_ten_percent);
                   }
   
                   if ($cart_contents_count >= 10) {
                       $woocommerce->cart->add_discount($coupon_ten_percent);
                   } else {
                       echo "less than 10";
                   }
               }
           }
       }
   
       add_action('wp_head', 'bulk_discount_coupons');
       ```
   
 * This code works as far as if the quantity is less than 10, the coupon is no longer
   applied and the total cart amount reflects. If the quantity is 10 or greater,
   the coupon is automatically applied and the total cart amount reflects. It works
   however, I don’t want the coupon to apply automatically. I just want it to be
   enabled.
 * With how the code is right now, the user isn’t required to enter the coupon code
   to receive the discount. Please help me figure out how to enable the coupon but
   not have it apply automatically. Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Support [Riaan K.](https://wordpress.org/support/users/riaanknoetze/)
 * (@riaanknoetze)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/programmatic-quantity-based-coupons/#post-12060219)
 * Hi there,
 * Out of interest, why don’t use the default coupons in WooCommerce by setting 
   a minimum spend amount necessary (equivalent to the value that 10 products would
   have)? You could further limit that to *specific* products if you have multiple
   products in the store which isn’t eligible.
 * I’m asking as that would mean you wouldn’t need to fiddle with 3rd party custom
   code 🙂
 *  Thread Starter [autifsnuc](https://wordpress.org/support/users/autifsnuc/)
 * (@autifsnuc)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/programmatic-quantity-based-coupons/#post-12061523)
 * Because if product X costs $3 and product Y costs $5, the solution will only 
   work in one of those cases. It isn’t an efficient solution.
 * If i can make a coupon quantity based, it will work regardless of the times 10
   calculated cost and therefore, regardless of the particular product. One coupon
   for many instead of many coupons for many products.
 * Also, i cannot use any additional plugins. That’s why I’m seeking a programmatic
   solution.
    -  This reply was modified 6 years, 7 months ago by [autifsnuc](https://wordpress.org/support/users/autifsnuc/).
 *  Plugin Support [Riaan K.](https://wordpress.org/support/users/riaanknoetze/)
 * (@riaanknoetze)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/programmatic-quantity-based-coupons/#post-12071281)
 * Thanks for clarifying that – admittedly it was entirely clear in the initial 
   thread 🙂
 * Given that this is a fairly complex development topic, I’m going to leave it 
   open for a bit to see if anyone is able to chime in to help you out.
 * I can also recommend the following places for more development-oriented questions:
    1. WooCommerce Slack Community: [https://woocommerce.com/community-slack/](https://woocommerce.com/community-slack/)
    2. Advanced WooCommerce group on Facebook: [https://www.facebook.com/groups/advanced.woocommerce/](https://www.facebook.com/groups/advanced.woocommerce/)
 *  [con](https://wordpress.org/support/users/conschneider/)
 * (@conschneider)
 * Engineer
 * [6 years, 7 months ago](https://wordpress.org/support/topic/programmatic-quantity-based-coupons/#post-12098236)
 * Hi there,
 * We haven’t heard back from you in a while, so I’m going to mark this as resolved–
   if you have any further questions, you can start a new thread.
 * Kind regards,

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Programmatic Quantity Based Coupons’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [cart](https://wordpress.org/support/topic-tag/cart/)
 * [code](https://wordpress.org/support/topic-tag/code/)
 * [discount](https://wordpress.org/support/topic-tag/discount/)
 * [quantity](https://wordpress.org/support/topic-tag/quantity/)

 * 4 replies
 * 3 participants
 * Last reply from: [con](https://wordpress.org/support/users/conschneider/)
 * Last activity: [6 years, 7 months ago](https://wordpress.org/support/topic/programmatic-quantity-based-coupons/#post-12098236)
 * Status: resolved