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!
PHP Snippet: Deny Further Sales If User Has Already Purchased a Product @ Shop / Single Product Page
This snippet will:
- Hide the Add to Cart completely on the single product page
- Rename the Add to Cart on the Shop Page to “Read More”
- 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;
}
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).
Hi Sandra, there is a filter indeed:
You can basically print whatever you like by setting your own custom $message
Hope this helps
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.
Hi Anoop! Try with
How to exclude multiple product ID?
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!
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??!?
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!
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.
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!
Thanks for the tips these are invaluable.. How do we tweak it only for a certain product category?
Hey Mac, I suggest you take a look at “conditional logic”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Enjoy 🙂
How can we replace it with view cart?
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!
Rodolfo, you are the best!
Thank you Gino!