Skip to content

Instantly share code, notes, and snippets.

@dryan1144
Last active November 26, 2020 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dryan1144/ed22b90bc8852ebe0f6f7252ccef2c9b to your computer and use it in GitHub Desktop.
Save dryan1144/ed22b90bc8852ebe0f6f7252ccef2c9b to your computer and use it in GitHub Desktop.
<?php
/***
Allow a product to be added only once to cart
***/
function drw_edd_one_item_checkout( $download_id, $options ) {
if ( edd_item_in_cart($download_id) ) {
$cart_contents = edd_get_cart_contents();
$cart_ids = array();
foreach( $cart_contents as $item) {
$cart_ids[] = $item['id'];
}
$cart_key = array_search ($download_id, $cart_ids);
edd_remove_from_cart( $cart_key);
set_query_var( 'edd_duplicate', 'duplicate' );
}
}
add_action( 'edd_pre_add_to_cart', 'drw_edd_one_item_checkout', 10, 2 );
/***
Set warning if duplicate is set
***/
function drw_edd_checkout_warnings() {
if ( get_query_var('edd_duplicate') ) {
printf('<div class="alert">%s</div>', __('This item can only be purchased once', 'drw'));
}
}
add_action( 'edd_before_checkout_cart', 'drw_edd_checkout_warnings' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment