WooCommerce Tutorial: Allow users only to purchase items once

WooCommerce slide new featured size

Ever had a store where you don’t want to let your users purchase the same item twice?

There are a few use-cases where this might be true, but whatever the root cause, if you have registered users on your WordPress and WooCommerce store, and it would be helpful for you to prevent customers from re-buying something they already purchased before, the snippet below might help you to get this done.

Please note you may want to add extra to this, like locking it to a product category or tag or specific product IDs. All of these features would be custom code which you’d want to ask a developer about (contact us or post a task onto Codeable!).

But if you just want to not allow any registered user from purchasing the same item twice, the below added to a functions.php file, or custom plugin, will get this done for you!

How to allow users only to purchase items once

// 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;
}
view raw gistfile1.txt hosted with ❤ by GitHub

This snippet should be added into your functions.php file, or even better, a plugin which contains such snippets, or perhaps a custom plugin.

Leave a comment if this helped you!

3 thoughts on “WooCommerce Tutorial: Allow users only to purchase items once

  1. Remember to replace the text “ERROR MESSAGE GOES HERE SORRY YOU CANNOT BUY ANYTHING TWICE OKAY!!” with your own message, relevant to your store and needs!

  2. Works great, if I wanted to replace the “Buy Now” button instead of having an error message, could I do that inside this code as well?

    1. Hi – what you’re describing could be achieved, but it would need to happen “before” the button is clicked (this code hooks into the “add to cart validation” filter `woocommerce_add_to_cart_validation`) therefore, you’d need to do this “some other way” – probably when the product page is loaded.

      It should be possible. Probably it would be worth posting this as a small task for a developer to complete – try Codeable (a dedicated WordPress freelancer service with only expert devs on it) – we have a form where you can send them a task here:

      https://silicondales.com/post-a-task-to-codeable/

      (note we will be marked as referrers, and we are “affiliated” i.e. this is a partner form, but the benefit – for our referrals – is this means I can share the task link to the great Woo devs on there I know to get someone with more availability than we to take a look!).

Leave a Reply

Your email address will not be published. Required fields are marked *