Woocommerce Hide Add to Cart for already purchased products
If you are a woocommerce store owner or developer then this article can be helpful for you. In this article, i will show you how you can hide add to cart button if logged-in users have already purchased that particular product.
Sometimes you want to hide add to cart button on woocommerce product page because if the user has already purchased the product then it doesn’t make sense to show him add to cart button for already purchased products. So, in this article, I will show you how you can do that without plugin and by using a simple snippet.
The snippet which I have shared in this article will work for all products and it will hide the add to cart button for all users who have already purchased the product. Just like the below image.
Finding the right plugin for your website can be a difficult and time-consuming process. There are over 55,000 free plugins available on WordPress.org and thousands of premium plugins available on third-party websites, so finding just the right solution for your needs can be overwhelming.
Unfortunately, not all plugins are created equal: some are poorly coded, others won’t work with your theme or other plugins, and many simply aren’t kept up-to-date.
If you don’t want to spend hours searching through different plugin directories, you should consider using a snippet instead. A snippet is a piece of code that can be added directly to your theme’s functions file in order to add new features or functionality to your website.
Steps to hide add to cart if product already purchased (without plugin)
You are going to edit your functions.php file. So, we have recommendations for you.
Take a full backup of your website or blog.
Be careful before editing the functions.php file. If you don’t want to edit functions.php directly, use the code snippet plugin.
We recommend you use a child theme before any customization on your website. You can use a plugin to create child theme
Also check:
Woocommerce Change Add to Cart Button Text
Woocommerce change Add to cart text if already at Cart
Woocommerce Hide Price & Add to Cart for Logged out users
1). Login to WordPress Admin Dashboard. Go to ‘Appearance’ then ‘Theme File Editor’. Then click on ‘Theme Functions’. This is the functions.php file of your website.
2). Just add below code snippet in the functions.php file and then hit ‘update File’.
//hide add to cart if product already purchased
add_filter( 'woocommerce_is_purchasable', 'wpamit_hide_add_cart', 9999, 2 );
function wpamit_hide_add_cart( $is_purchasable, $product ) {
if ( wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) {
$is_purchasable = false;
}
return $is_purchasable;
}
And it’s done. I have tested this snippet in many of my client’s websites and it is working perfectly fine. If you face any error or other issue, please leave a comment below. I will be happy to help you.