The most simple way to make all your products unable to be purchased is to add the following line to your functions.php in your WordPress theme, add_filter( ‘woocommerce_is_purchasable’, ‘__return_false’);
There are however other ways to do this as well so read on to see how to do this for specific products but also via CSS and jQuery if indeed this suits your use case.
Table of Contents
- Define ‘non purchaseable’
- Various methods to make a product non-purchasable
- Learn to customize WooCommerce Fully with an online Udemy course
Definition of ‘non purchaseable’
In this article I take non purchasable to mean either removing the product from public facing site completely or simply hiding the products ‘add to cart’ button. In this way you can ensure that either nobody can access it to add it to their cart of they physically cant add it to their cart at all but can still see the product page.
Various methods to make a product non-purchasable
Hide ‘Add to Cart’ button
With CSS or jQuery you could hide all the add to cart buttons or just a specific one also.
Here is the CSS to do that:
// hides all add to cart buttons everywhere
.add_to_cart_button, .single_add_to_cart_button {display:none !important}
// hides a specific add to cart button
.post-123456 .add_to_cart_button {display:none !important}
#product-123456 .single_add_to_cart_button {display:none !important}
NOTE: Make sure to replace the above ‘123456’ with the product ID for your particular product.
You could add the above to your style.css in the root of your WordPress theme at something similar to https://yoursite.com/wp-content/themes/yourtheme/style.css
If you’d like to do this in jQuery you can do something like this,
jQuery(document).ready(function($) {
// hides all add to cart buttons
$('.add_to_cart_button').hide();
$('.single_add_to_cart_button').hide();
// hide one specific add to cart button for a particular product
$('.post-123456 .add_to_cart_button').hide();
$('#product-123456 .single_add_to_cart_button').hide();
});
NOTE: Make sure to replace the above ‘123456’ with the product ID for your particular product.
You could add this to your script.js which will be at something like https://yoursite.com/wp-content/themes/yourtheme/script.js
Unpublish or change visibility the Product(s)
If you wanted to you could simply ‘unpublish’ the product in the CMS.
First you would find the product in your CMS at, https://yoursite.com/wp-admin/edit.php?post_type=product and then click into it.
Then, on the right hand side of the CMS you will see the two options. The first to unpublish and the second the make private with the visibility settings.
Make the changes and hit save and the product should be hidden from the site.
NOTE: This wont hide all products obviously just this particular one.
Programmatically stop all products from being purchasable
If you wanted to, via PHP, remove all add to cart buttons from all parts of the site you could simply add the PHP below to your funcitons.php file at https://yoursite.com/wp-content/themes/yourtheme/functions.php
<?php
// existing code in functions.php
add_filter( 'woocommerce_is_purchasable', '__return_false');
To explain this WooCommerce calls this filter before printing out the ‘add to cart’ button and if false is returned as in the above example no button will be printed out.
NOTE: Just remove when you want the buttons to show again.
Programmatically stop a specific product from being purchasable
We can also do something similar to the above if we want to remove, via PHP, the add to cart button for specific products only.
You could add, and alter, the below code to your functions.php file at https://yoursite.com/wp-content/themes/yourtheme/functions.php
NOTE: Make sure to alter the product IDs in the array in the if statement. You can find the product IDs for the specific products in the CMS by going to your list of products at, https://yoursite.com/wp-admin/edit.php?post_type=product. The ID will be printed out in each product row.
<?php
// existing code in your functions.php
add_filter('woocommerce_is_purchasable', 'removeSomeProducts', 10, 2);
function removeSomeProducts($is_purchasable, $product ) {
global $product;
if( in_array( $product->get_id(), array(123, 456, 678) )) {
return false;
}
return $is_purchasable;
}
Learn to customize WooCommerce Fully with an online Udemy course
If you’d like to know more about how to customize WooCommerce completely check out these courses from Udemy which are low cost, online and have been completed by thousands of people.