Creating Custom Add to Cart Links
Many times when building a site or theme with Easy Digital Downloads, you will want to create custom purchase/add-to-cart buttons that allow your users to purchase a digital product. Sometimes the button options that come with EDD are not quite suited to your application. Luckily, there is a very simple way to turn any hyperlink into a “purchase button”.
Creating the link
Let’s say that you have a hyperlink displayed on your product details that says “Purchase This”, and you have styled it exactly as you want. The only thing you have left is to turn the link into one that automatically adds the current item to the shopping cart. It’s very simple and the basic format looks like this:
http://example.com/?edd_action=add_to_cart&download_id=ID_OF_THE_DOWNLOAD
This
ID_OF_THE_DOWNLOAD
would be replaced with the actual ID number of your download.
You can find the ID of your download by going to Downloads → All Downloads and then clicking on the product you’re looking for. Then in the URL you’ll find the ID you’re looking for. Example:
Redirect to checkout
If, when clicked, you want the link to automatically take the user to the checkout page, do this:
http://example.com/checkout/?edd_action=add_to_cart&download_id=ID_OF_THE_DOWNLOAD
“http://example.com/checkout” would be replaced with the full URL to your checkout page.
If you are displaying the link on the single page for the “download” post type, then you can do this (it will automatically append the ID):
<a href="http://example.com/checkout/?edd_action=add_to_cart&download_id=<?php echo get_the_ID(); ?>">Purchase This</a>
If you want to create a link for a specific price option, you can do it like this:
http://example.com/checkout/?edd_action=add_to_cart&download_id=DOWNLOAD_ID&edd_options[price_id]=PRICE_ID
Note, variable prices must be enabled on your product in order for the price ID parameter to be included.
Sending customers straight to PayPal
If you’d like your customers to be sent straight to PayPal, instead of to the checkout page, use a URL like this, replacing ‘100’ with your product ID:
http://example.com/checkout/?edd_action=straight_to_gateway&download_id=100
Below is an example of using the WordPress functionget_the_ID()
to automatically get the product ID and append it to the URL.
<a href="http://example.com/checkout/?edd_action=straight_to_gateway&download_id=<?php echo get_the_ID(); ?>">Purchase This</a>
Including an item quantity
If you want to specify the quantity that should be added to the cart, make sure you have Item Quantities enabled (Downloads -> Settings -> Misc) and build your URL like this:
http://example.com/checkout/?edd_action=add_to_cart&download_id=DOWNLOAD_ID&edd_options[quantity]=QTY
Including a discount code
Full docs on how to add a discount code to a link are found here. If you want to add both a discount code and a download ID at the same time it would look something like this:
http://example.com/?edd_action=add_to_cart&download_id=ID_OF_THE_DOWNLOAD&discount=CODE