Last active
November 26, 2020 06:08
-
-
Save dryan1144/ed22b90bc8852ebe0f6f7252ccef2c9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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