Easy Digital Downloads is a great eCommerce plugin for anyone who wants to sell any digital or downloadable goods. When creating custom solutions with EDD, you will probably have to add some custom data to the cart. In this tutorial, you’ll learn that.

The Cart Class

You can learn a lot on how to manage the cart from Easy Digital Downloads from the EDD_Cart object. You can locate that class in the file includes/cart/class-edd-cart.php.

There are several filters or actions you can use to add custom data. If you don’t know what filters or actions are in WordPress, I want to invite you to join my free email course on How to Become a WordPress Developer.

Here are some of the filters or actions you could check out:

Filters:

  • edd_cart_contents – Filter the contents of the cart
  • edd_pre_add_to_cart_contents – Filter the contents before a new item has been added
  • edd_add_to_cart_item – Filter the item that will be added to the cart
  • edd_get_cart_item_discounted_amount – Filter the discount amount
  • edd_item_in_cart – Filter the boolean when we check if the item is already in the cart
  • edd_cart_item_price_label – Filter the price label in the cart
  • edd_cart_item_price – Filter the Item price (excl. tax or discounts)
  • edd_cart_item_final_price – Filter the final Item price (incl. tax and discounts)
  • edd_cart_total – filter the cart total
  • edd_get_cart_item_name – Filter the Item name

And so on and so on. There are a lot more filters but I think, you get the idea on how the cart be extended and filtered with the Filter hooks.

Actions:

  • edd_cart_contents_loaded_from_session – Do some action when the cart is loaded from the session. Perfect if you saved something after the cart has been saved in the session.
  • edd_cart_contents_loaded – The cart contents have been loaded.
  • edd_pre_add_to_cart – Triggers before the new item has been added to the cart
  • edd_post_add_to_cart – Triggers after the new item have been added to the cart. Also, after the cart contents have been saved to the session.
  • edd_pre_remove_from_cart – Do something before we remove an item from the cart
  • edd_post_remove_from_cart – The item has been removed from the cart. Do something.
  • edd_empty_cart – The cart has been emptied. Maybe you need to delete something else that is related to the cart?

I’ll stop here. There are a lot of other actions. Be sure to check the class!

Adding Data to the Cart

For this example, I will show you how to add an ID from a related product to the Cart of the current Item we want to add.

In this example, we are checking for a parameter questionnaire_id. If that parameter exists in the $_GET or $_POST, then we will add that questionnaire ID to that item.

We could add that data as a direct key to the item, but we are saving that ID in the options array. Why? EDD has a function edd_item_in_cart that will check the item ID (download ID) and the options array to see if the same item (with all options) has already been added to the cart.

This example has been copied from my new eBook Easy Digital Downloads for Developers.

Check the Item against the Same Custom Data

If you are working with unique products, you don’t want to have 2 items of the same custom data. We can do that with the filter edd_item_in_cart.

Here we check if the item is already in the cart. If it is, we are then checking if we have the same custom data. If the same custom data is not there, we will return false.

This example has been also copied from my new eBook Easy Digital Downloads for Developers.

Managing the Related Product on Purchase

If there is a need for our related product to change its status or something else, then we need to perform that action when the purchase was completed. Here is a simple example:

So, you just check if that custom data is there. If it is, perform some action on it. In my new eBook we would save that ID to our user and also update its status so we know that this related product has been bought.

Conclusion

Managing Cart Data in Easy Digital Downloads is really simple. Also, all that data is provided to you within orders. In my experience, it is much easier to manage such data in Easy Digital Downloads than WooCommerce. WooCommerce performs differently, so you also need to use the actions for session etc.

If you are interested in learning more on developing with Easy Digital Downloads, be sure to check my eBook “Easy Digital Downloads for Developers“.

Become a Sponsor

Posted by Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.