WooCommerce: Hide Add to Cart If Already Purchased

We already saw how to hide add to cart for logged out users and how to find out if a user has already bought a given product – so I said why not combine the two snippets and figure out how to hide the add to cart button if a logged in customer has already purchased a product?

After that, however, I realized that the “woocommerce_is_purchasable” filter offered by the WooCommerce plugin makes the task much easier than just combining the two mini-plugins above.

So, here’s how it’s done – enjoy!

In case a customer is logged in, and has purchased a product, the Shop page will not show the “Add to Cart” button, but a “Read more” instead
The single product page, also, won’t show the Add to Cart button at all (once again, in case the current logged in user has purchased that product already)

PHP Snippet: Deny Further Sales If User Has Already Purchased a Product @ Shop / Single Product Page

This snippet will:

  1. Hide the Add to Cart completely on the single product page
  2. Rename the Add to Cart on the Shop Page to “Read More”
  3. Make it impossible to add the item to cart even with an URL – it will show a red “Sorry, this product cannot be purchased.” error in such case
/**
 * @snippet       Hide Add Cart If Already Purchased - WooCommerce
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 5
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_is_purchasable', 'bbloomer_hide_add_cart_if_already_purchased', 9999, 2 );

function bbloomer_hide_add_cart_if_already_purchased( $is_purchasable, $product ) {
	if ( wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) {
		$is_purchasable = false;
	}
	return $is_purchasable;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

16 thoughts on “WooCommerce: Hide Add to Cart If Already Purchased

  1. I am finally using your code as a way to hide the cart, but I was wondering it there is any way to change the default cart message to a custom message instead of “PRODUCT NAME has been removed from your cart because it can no longer be purchased. Please contact us if you need assistance.” The message may generate lots of emails to me. I thought it was worth asking. I currently have just a message appear when logged in has already purchased within the shop view (your code as a base).

    1. Hi Sandra, there is a filter indeed:

      $message = apply_filters( 'woocommerce_cart_item_removed_message', $message, $product );

      You can basically print whatever you like by setting your own custom $message

      Hope this helps

  2. Hi,
    I have used below code for one time product purchase, code is working fine, but I want to exclude wallet (Product ID 1417) from this code. Please suggest me, how to exclude wallet from this code.

    1. Hi Anoop! Try with

      if ( wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) && 1417 !== $product->get_id() ) {
      1. How to exclude multiple product ID?

        1. Hello Franster, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Hi,

    I’m looking for a way to completely hide a purchased product for logged in users. I’m adding around 300 subscriptions to my site with Woo Commerce Subscriptions and it would be great if my customer who has 200 of them will only see the 100 that are left. Not in the store, but to be able to specify it in a grid/masonry. In my world this is essential and something they missed in Subscriptions, but also core. Any ideas??!?

    1. Hi Patrik thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  4. Could this easily be extended to variable products? I’d love it if I could hide “add to cart” on a variable product upon purchase of certain attributes and eliminate all variations upon a purchase of a different attribute.

    1. Hi James thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  5. Thanks for the tips these are invaluable.. How do we tweak it only for a certain product category?

    1. Hey Mac, I suggest you take a look at “conditional logic”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Enjoy 🙂

  6. How can we replace it with view cart?

    1. Hi Fabian, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  7. Rodolfo, you are the best!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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