Skip to content

Instantly share code, notes, and snippets.

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 jennlee20/745c700471723f654efac60459051f92 to your computer and use it in GitHub Desktop.
Save jennlee20/745c700471723f654efac60459051f92 to your computer and use it in GitHub Desktop.
<?php
function jennsup_custom_checkout_product_validation() {
global $woocommerce;
$cart_items = $woocommerce->cart->cart_contents;
$restrict_product_id = 9787; //your restricted product id
foreach ( $cart_items as $key => $item ) {
$product_id = $item['product_id'] == 0 ? $item['variation_id']: $item['product_id'];
$product_qty = $item['quantity'];
if($product_id == $restrict_product_id) {
if ($product_qty > 1)
wc_add_notice("Sorry, you only can purchase 1 quantity for this item.", 'error');
$customer_email = sanitize_text_field($_POST['billing_email']);
//check email purchase before?
$repeat_order = wc_customer_bought_product( $customer_email, get_current_user_id(), $product_id );
if($repeat_order)
wc_add_notice("Sorry, you had purchased this special item before.", 'error');
}
}
}
add_action('woocommerce_checkout_process','jennsup_custom_checkout_product_validation');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment