Here is my problem: I have a vehicle parts site which lets user select their specific model and be saved via a cookie. Then the whole site will be filtered via my module plugin, where I override “Magento/Catalog/Model/ResourceModel/Product/Collection” with the aroundPrepareProductCollection function where I “addattributetofilter” if cookie exists.

I have a megamenu set up on a phtml file with a bunch of categories as an html/css dropdown. (I can provide code.) My issue is that certain vehicle fitments won’t have any products in a few categories. MAIN QUESTION: How can I check whether products exist in a category that’s been filtered, and show them if there are products, and hide if there are not? Kind of like how a layered navigation works (if there is > 0 products in an attribute, show the attribute. If not, don’t show.) My main method I’m thinking is just editing the phtml that contains the megamenu, and add some php in it. But are there other more efficient methods?

The reason this is important is that so users won’t be able to click on a link in the mega menu (which are categories and sub-categories respectively), and then get shown that “We can’t find products matching your selection” aka no products in this category fit your vehicle aka turns people off site. When we can just show them relevant categories in megamenu. Thanks in advanced!!

1 Answer 1


I think I would do one calculation when the customer selects a model: get a list of categories which contain products for the selected models. It's a one time calculation, I'd make it an AJAX call in the background and store the result (the list of category IDs as JSON) in a cookie or in local storage. Then you can have a JavaScript function (i.e. RequereJS and jQuery). It can be called on every page and will hide all categories which are not in the list of allowed category IDs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.