Difficulty: Easy
In the last post we covered how to make certain products not purchasable by using their product IDs, but what about making certain categories not purchasable. Well we will cover that right now. Making certain categories not purchasable in your WooCommerce store is actually not that much different then making only few products not purchasable. To achieve this you just need to add some code to your functions.php file.
Table of Contents
TLDR SECTION
TLDR
File: Functions.php
add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);
function my_woocommerce_is_purchasable($is_purchasable, $product) {
$product_category = array('<product_category_name>','<product_category_name>');
$product_category_check = $product->get_categories();
foreach($product_category as $value){
if(strpos($product_category_check, $value)){
return false;
}else{
return true;
}
}
}
*Don’t forget to replace <product_category_name> with the the name of a category you want to make not purchasable
Finding name of the product categories
To find the name of the category your products belong to you need to navigate to product list using admin interface. On the admin interface there should be a menu where it says Products. Just click on it.
After navigating there you will see a window like this one below. In there you just need to find the product/products you want to make not purchasable and look under categories. There you will see all the categories to which that product belongs to. Write down or remember the name of category and move on to the next step.
Now that you have written down or remembered the name of the product category you want to make not purchasable. You need to navigate to your functions.php file.
Making categories not purchasable
Go to the admin interface and then go to Appearance and then under Appearance select Theme Editor
After navigating there you will see a window like this one below.
In there select Theme Functions (functions.php) and then at the end of the file put this code below.
add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);
function my_woocommerce_is_purchasable($is_purchasable, $product) {
$product_category = array('<product_category_name>','<product_category_name>');
$product_category_check = $product->get_categories();
foreach($product_category as $value){
if(strpos($product_category_check, $value)){
return false;
}else{
return true;
}
}
}
Where it says <product_category_name> insert your category name which you want to make not purchasable. If you have only one category that line of code will look something like this:
$product_categoy = array(‘CG odobreno’);
If you have multiple categories that you need to make not purchasable that line of code will look similar to this:
$product_categoy = array(‘CG odobreno’,’Styling’);
In case you have any questions or need help implementing this solution on your website, please leave a comment down below and WooWiki team will help you as soon as possible.
2 Comments
I did all the steps but it doesn’t seem to work for me. What can be the problem?
If i set a category, does it affect all the subcategories which are in the parent category? Or should I set all the categories, no matter if they are sub- or parent categories?
I have a lot of categories which I would like to do this to.
Hi,
Do you use some plugins that built on top of WooCommerce functionality such as Role-based Pricing Plugin or WooCommerce Catalog Mode cause they are maybe causing the issue with this solution.
If so you need to add this code to other filter that those plugins use.
We can help you with that you just need to give us the name of the plugin you use.
To answer the other question:
If you set a parent category not purchasable it will only affect products that are “tagged” with both child and parent categories or only parent category.
If you have products that are only tagged with child categories and not with a parent category you will need to insert all child categories in this code snippet.