Automatically Apply an EDD Discount Code Based on Cart Contents

This tutorial can probably be achieved with the Discounts Pro extension (affiliate link). And while I’m all for supporting the folks at EDD, sometimes you just don’t feel like shelling out money for something.

I struggled for a silly amount of time with whether or not to buy that extension. I wanted to automatically add a discount code if someone was buying the Ultimate Book Blogger plugin and a WordPress theme or UBB add-on. But… I was trying to spend less money and just didn’t feel like paying for yet another add-on.

Then I finally realized it probably wouldn’t be that hard to code myself. And what do you know.. it was pretty easy.

My Logic

Your logic may vary from mine, but here were the conditions I wanted to enforce:

The discount code would be added if the Ultimate Book Blogger plugin was in the cart and at least one of the following conditions applied:

  • Any WordPress theme was also in the cart (products in the “WordPress Themes” download category).
  • Any UBB extension was also in the cart (products with the “UBB Add-On” tag). But I wanted to exclude free extensions, for obvious reasons.

Step 1: Create a discount code

First create a discount code in EDD with no restrictions. We’ll be applying the restrictions ourselves via code.

Step 2: Add the function

This function can be placed inside a new plugin file or in your theme’s functions.php file. Whatever you want.

Now let’s check out my code.

/**
 * Maybe Apply 20% Discount
 *
 * Automatically applied if UBB is in the cart and at least one theme or paid add-on.
 *
 * @return void
 */
function ng_edd_maybe_apply_discounts() {

    /* Code will go in here */

}

add_action( 'edd_cart_items_before', 'ng_edd_maybe_apply_discounts' );

Let’s add in some code where I designated piece by piece.

Set up some variables.

Get the contents of the cart with this:

$cart_items = edd_get_cart_contents();

Then set up your discount code in a variable. We’ll be using it later. Here’s mine:

$discount_code = 'UBB_Bonus';

That’s the code you created from before.

Now for my logic, I created four variables and set them all to false.

$discount_applies = $has_ubb = $has_theme = $has_addons = false;
  • $discount_applies – Whether or not we should apply the discount.
  • $has_ubb – Whether or not Ultimate Book Blogger is in the cart.
  • $has_theme – Whether or not a theme is in the cart.
  • $has_addons – Whether or not UBB add-ons are in the cart.

So eventually our logic is going to work like this:

// Has UBB and a theme.
if ( $has_ubb && $has_theme ) {
	$discount_applies = true;
}

// Has UBB and one or more add-ons.
if ( $has_ubb && $has_addons ) {
	$discount_applies = true;
}

if ( $discount_applies ) {
	// Do something to add the coupon code.
}

But before we actually do that part, we need to loop through each item in the cart and check to see if $has_ubb, $has_theme, or $has_addons should be switched over to true. Check it out:

Check to see if the conditions are met

if ( is_array( $cart_items ) ) {
	foreach ( $cart_items as $item ) {
		$post         = get_post( $item['id'] );
		$currentprice = get_post_meta( $post->ID, 'edd_price', true );

		// If the item ID is 8779 then it's the Ultimate Book Blogger plugin.
		if ( $item['id'] == 8779 ) {
			$has_ubb = true;
		}

		// If the item has the "WordPress Themes" category, then we have a theme.
		if ( has_term( 'WordPress Themes', 'download_category', $post ) ) {
			$has_theme = true;
		}

		// If the item has the "UBB Add-On" tag and the price is greater than zero, we have an add-on.
		if ( has_term( 'UBB Add-On', 'download_tag', $post ) && $currentprice > 0 ) {
			$has_addons = true;
		}
	}
}

If they are, apply the discount. If not, remove it.

Now we can finish it up with that code from before:

// Has UBB and a theme.
if ( $has_ubb && $has_theme ) {
	$discount_applies = true;
}

// Has UBB and one or more add-ons.
if ( $has_ubb && $has_addons ) {
	$discount_applies = true;
}

if ( $discount_applies ) {
	edd_set_cart_discount( $discount_code );
} else {
	edd_unset_cart_discount( $discount_code );
}

So if the conditions match, we use edd_set_cart_discount( $discount_code ) to set the discount code. If they don’t match, we use edd_unset_cart_discount( $discount_code ) to remove the discount code. That’s important to account for someone meeting the conditions then removing an item from their cart and no longer qualifying.

Check out the code on GitHub

Note: Make sure you’re on EDD 2.6.7 because that version fixes a glitch with unsetting the cart discount. My first ever EDD pull request! Go me!

Another example based on number of items in the cart

Here’s another example that may more directly apply to you: we’ll add the discount code if three or more items are in the cart. This one is even easier! Here’s the full code:

/**
 * Maybe Apply 20% Discount
 *
 * Automatically applied if three or more items are in to the cart.
 *
 * @since 1.0
 * @return void
 */
function ng_edd_maybe_apply_discounts() {
	$cart_items       = edd_get_cart_contents();
	$discount_code    = 'BIGPURCHASE'; // Change this to your custom discount code.
	$discount_applies = false;
	
	if ( is_array( $cart_items ) && count( $cart_items ) >= 3 ) {
		$discount_applies = true;
	}
	
	if ( $discount_applies ) {
		edd_set_cart_discount( $discount_code );
	} else {
		edd_unset_cart_discount( $discount_code );
	}
}

add_action( 'edd_cart_items_before', 'ng_edd_maybe_apply_discounts' );

P.S. Automatically get 20% off when you buy UBB + any theme

Start Shopping »

Photo of Ashley
I'm a 30-something California girl living in England (I fell in love with a Brit!). My three great passions are: books, coding, and fitness. more »

Don't miss my next post!

Sign up to get my blog posts sent directly to your inbox (plus exclusive store discounts!).

You might like these

One comment

  1. Hi Ashley ,
    Very informative post. it’s been a huge help to me 🙂
    if possible can you please suggest how to apply discount only to products in a specific download-category in the cart?
    i’ve also noticed that while the checkout page cart total amount is correct, the header cart amount (i’m using the vendd theme) does apply the discount without doing a page refresh. Can you please suggest how to get this working?

    Many Thanks,
    yan

Recent Posts

    Random Posts