Last active
March 23, 2021 22:45
-
-
Save WillBrubaker/e3515f2f7b921fa1c370f87d67f47155 to your computer and use it in GitHub Desktop.
Prevent WooCommerce guest checkout from repeat purchase of a particular product
This file contains hidden or 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
//not sure what to do with this code snippet? See https://www.thathandsomebeardedguy.com/what-do-i-do-with-these-code-snippets/ | |
//BTC Donations to: bc1qc2s60yct2aqza4r7ryweheepd8xa8wqpfgdhg3 | |
//Hook into the after checkout validation action, add a callback function named 'validate_no_repeat_purchase' with a priority of 10 and pass two arguments. | |
add_action( 'woocommerce_after_checkout_validation', 'validate_no_repeat_purchase', 10, 2 ); | |
//The callback function accepts the array of posted checkout data and an array of errors | |
function validate_no_repeat_purchase( $data, $errors ) { | |
//extract the posted 'billing_email' and use that as an argument for querying orders. | |
$args = array( | |
'customer' => $data['billing_email'], | |
); | |
//loop through the cart items to see if one is the product that doesn't allow repeat purchases. | |
foreach( WC()->cart->get_cart() as $cart_item ) { | |
$product_id_no_repeat_purchases = 666;//substitute your product id. | |
if ( $cart_item['product_id'] === $product_id_no_repeat_purchases ) {//note the identical operator. | |
//use the WooCommerce helper function to query for orders with this email address: | |
$orders = wc_get_orders( $args ); | |
//if an order with this email exists, don't allow another one. | |
if ( $orders ) { | |
$errors->add( 'validation', '<strong>Repeat order not allowed.</strong>' ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment