How to Force Maximum 1 Category Per Product WooCommerce

WooCommerce Force Maximum 1 Category Per ProductAre you looking for a way to force a maximum of one category for each product in your store? Then read on, as this post will provide you with the best solution to this problem. We will be using a custom code snippet we made specifically to solve this problem.

It is important to note that WooCommerce allows you to create and add unlimited categories to each product in your store. Using product categories is a great way to de-clutter your online store.

Product categories intuitively guide users to the page they are looking for. This means that customers will not waste a lot of time searching for a product.

If you have a very big store like a multi-vendor store, you might want to restrict the number of categories per product. This will bring uniformity, and you will be able to track down all products in your store.

However, WooCommerce does not have a built-in solution to change this functionality. This is why we created this post to help you out.

WooCommerce Force Maximum 1 Category Per Product

In this brief tutorial, you will learn how to force a maximum of one category per product. Since we will be using a custom code snippet, you need to have some coding skills. But we will explain each step in detail to make it easy for you to implement the solution.

Before you proceed, you should install or create a child theme. This will ensure that your changes are not lost during an update.

Let us get right into it.

Steps to Force Maximum 1 Category Per Product

Here are the simple steps you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file to add the function to force a maximum of one category per product.
  3. Add the following code to the php file:
[php] add_action( ‘admin_head’, ‘njengah_max_one_product_category’, 9999 );
function njengah_max_one_product_category() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : ”;
if ( ( ‘add’ === $screen->action && ‘product’ === $_GET[‘post_type’] ) || in_array( $screen_id, array( ‘product’, ‘edit-product’ ) ) ) {
wc_enqueue_js( "
$(‘#product_cat-all input:checkbox’).change(function () {
var max = 1;
var count = $(‘#product_cat-all input:checked’).length;
if (count > max) {
$(this).prop(‘checked’, ”);
alert(‘Sorry, you can only pick one category. Please untick current category and pick another one in case you wish to switch category.’);
}
});
" );
}
}

[/php]
  1. This is the outcome:error

Wrapping Up

By now, you should be able to force a maximum of one category per product in your WooCommerce store. However, you can define your own maximum number.

Since we are modifying WooCommerce’s core files, we recommend that you fully backup your site. This will help you revert to the previous version if you make a mistake. You should also be careful when editing the functions file. If you make any mistake, a critical error will be displayed on your site.

Similar Articles