I am experiencing the issue that empty categories are still showing. I would like to have them appear an disappear automatically when products are present or not. Is that possible?
Thank you for purchasing our plugin! I understand you'd like to automatically hide empty categories and show them only when they contain products.
Please go to B2BKing -> Settings -> Other, and enable the "Product Visibility Cache" option. In most cases, this setting should help hide empty categories completely.
However, the exact behavior may depend on the specific widget, theme, or page builder you're using to display the categories. If enabling this setting doesn't solve the issue for you, could you please: 1. Let me know where exactly these categories are showing (menu, sidebar, specific pages, etc.) 2. Share a screenshot if possible 3. Let me know which theme and page builder you're using
Sorry here I am again. I have a piece of code that hides menu links to empty categories and that works. However for that the category has to be actually empty, not only for B2B customers. If there is no other soltion would it be possible to tweak this code so that it executes the code wehenever it has no B2B products?
// Add class to menu item if product category is empty function add_class_to_empty_category_menu_item($classes, $item) { if ($item->object == 'product_cat') { $category_id = $item->object_id; $category = get_term($category_id, 'product_cat');
if (empty($category->count) || intval($category->count) === 0) { $classes[] = 'niksdrin'; // Add your custom class here } } return $classes; } add_filter('nav_menu_css_class', 'add_class_to_empty_category_menu_item', 10, 2);
-> It is normal that the visibility cache setting results in 404 errors when those category pages are accessed directly. That's because it removes the categories at a very basic level before WooCommerce is initialized and thus it's like they do not exist - which is why it can also work to hide them from menus.
However, indeed we do not have a good way to both prevent 404s there and also hide empty categories from menus.
This code works well for the menu in the header but menu-items also appear in widgets and in pages as thumbnails. Is there a way to add the class to all instances of a category?
hello,
I am experiencing the issue that empty categories are still showing. I would like to have them appear an disappear automatically when products are present or not. Is that possible?
greetings,
marcel
Hi Marcel,
Thank you for purchasing our plugin! I understand you'd like to automatically hide empty categories and show them only when they contain products.
Please go to B2BKing -> Settings -> Other, and enable the "Product Visibility Cache" option. In most cases, this setting should help hide empty categories completely.
However, the exact behavior may depend on the specific widget, theme, or page builder you're using to display the categories. If enabling this setting doesn't solve the issue for you, could you please:
1. Let me know where exactly these categories are showing (menu, sidebar, specific pages, etc.)
2. Share a screenshot if possible
3. Let me know which theme and page builder you're using
Kind regards,
Stefan
Hello,
When I enable the cache I get this result on every category page:
When I disable cache it works but empty categories remain in the main menu. I am using the Porto theme, latest version.
Thank you,
Marcel
hello,
I get a 404 on empty category pages when I enable cache.
When I disable cache it works but categories are showing. I am using Porto theme
hello,
Sorry here I am again. I have a piece of code that hides menu links to empty categories and that works. However for that the category has to be actually empty, not only for B2B customers. If there is no other soltion would it be possible to tweak this code so that it executes the code wehenever it has no B2B products?
// Add class to menu item if product category is empty
function add_class_to_empty_category_menu_item($classes, $item) {
if ($item->object == 'product_cat') {
$category_id = $item->object_id;
$category = get_term($category_id, 'product_cat');
if (empty($category->count) || intval($category->count) === 0) {
$classes[] = 'niksdrin'; // Add your custom class here
}
}
return $classes;
}
add_filter('nav_menu_css_class', 'add_class_to_empty_category_menu_item', 10, 2);
Hope to find a solution. Thank you,
Marcel
Hi Marcel,
Thank you for your patience on this,
-> It is normal that the visibility cache setting results in 404 errors when those category pages are accessed directly. That's because it removes the categories at a very basic level before WooCommerce is initialized and thus it's like they do not exist - which is why it can also work to hide them from menus.
However, indeed we do not have a good way to both prevent 404s there and also hide empty categories from menus.
-> The solution I'd recommend would also be using some CSS, similar to what you are doing. With B2BKing you can add group-specific CSS that only applies to specific groups, or only for B2B users, etc, as described at https://woocommerce-b2b-plugin.com/docs/shortcode-to-restrict-content-to-b2b-b2c-user-groups-specific-users/
Specifically regarding the code you shared, you can replace it with:
It will automatically add that class only when the visibility is set as disabled in category settings: https://prnt.sc/KBLpAphZZQGX
So the class should be added if the current user is B2B and their group does not have the visibility checkbox enabled for the category.
(we cannot do it based on the actual 'count' property because that's difficult to calculate)
Kind regards,
Stefan
hello Stefan,
Thanks!
This code works well for the menu in the header but menu-items also appear in widgets and in pages as thumbnails. Is there a way to add the class to all instances of a category?
greetings,
Marcel