Easy Digital Downloads: Only allow max quantity of 1

Here on WPSunshine I use Easy Digital Downloads to sell my plugins. I do not allow customers to purchase multiples of the same plugin. I offer all Sunshine Photo Cart paying customers a free Confetti single site license via a discount code with a unique URL. However, clicking that URL multiple times meant someone could add multiple single site licenses and get each one free – thus allowing unlimited site licenses.

The below code prevents this from happening by checking if the item added to the cart already exists and not allowing another instance of it to be added to the cart thus only ever allowing a single site license to be purchased. The “Use Once Per Customer” option for the discount code will then prevent the user from getting multiple free licenses on multiple purchases.

Put the following in your theme’s functions.php file or create a custom mini plugin in WordPress:

 add_filter( 'edd_add_to_cart_item', 'wpsunshine_limit_qty' );
 function wpsunshine_limit_qty( $item ) {
     $cart_items = edd_get_cart_contents();
     foreach ( $cart_items as $cart_item ) {
         if ( $cart_item['id'] == $item['id'] ) {
             return false;
         }
     }
     return $item;
 }
About the Author
Derek Ashauer is the sole developer at WP Sunshine. He also does client work through his agency AshWebStudio where he has been working with WordPress since 2005.