Skip to content

Instantly share code, notes, and snippets.

@robin-scott
Last active February 12, 2019 10:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robin-scott/4c8ea6bba196129172d2bd876b637b8a to your computer and use it in GitHub Desktop.
Save robin-scott/4c8ea6bba196129172d2bd876b637b8a to your computer and use it in GitHub Desktop.
Make woocommerce not allow a product user has purchased before to be purchased again
// posted by Robin Scott https://silicondales.com/tutorials/woocommerce-tutorials/woocommerce-tutorial-allow-users-purchase-items/ - prevent user adding item to cart if they bought it before...
add_filter('woocommerce_add_to_cart_validation','sd_bought_before_woocommerce_add_to_cart_validation',20, 2);
function sd_bought_before_woocommerce_add_to_cart_validation($valid, $product_id){
$current_user = wp_get_current_user();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id)) {
wc_add_notice( __( 'ERROR MESSAGE GOES HERE SORRY YOU CANNOT BUY ANYTHING TWICE OKAY!!', 'woocommerce' ), 'error' );
$valid = false;
}
return $valid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment