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        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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 PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my guide "Should I Add Custom Code Via WP Editor, FTP or Code Snippets?" and my video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything went as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting.

If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

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.

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? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. 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 BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

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