Indexing
...
Entities
Categories
Exclude Categories from Search Results
6min
by default, all categories indexed to klevu have a visibility of search there are 2 possible approaches to change this depending on your needs the first is to exclude the category from the sync altogether using an isindexablecondition the second is to change the visibility sent in the data is indexable condition excluding the category this way would result in no products showing for this category on the frontend of your store for category navigation to do this we inject an isindexablecondition into the isindexableconditions array for the virtual type klevu\indexingcategories\service\determiner\isindexabledeterminer vendor/module/etc/di xml \<virtualtype name="klevu\indexingcategoriess\service\determiner\isindexabledeterminer"> \<arguments> \<argument name="isindexableconditions" xsi\ type="array"> \<item name="pageisindexablecondition" xsi\ type="object">vendor\module\service\determiner\categoryidisindexablecondition\</item> \</argument> \</arguments> \</virtualtype> create that class, which must implement the isindexableconditioninterface vendor/module/service/determiner/categoryidisindexablecondition php \<?php namespace vendor\module\service\determiner; use klevu\indexingapi\service\determiner\isindexableconditioninterface; use magento\catalog\api\data\categoryinterface; class categoryidisindexablecondition implements isindexableconditioninterface { / edit routes as required alternatively inject these via di xml @param int\[] / private array $excludedcategories = \[ 4, 23, ]; public function execute( extensibledatainterface|pageinterface $entity, storeinterface $store, ) bool { if (!($entity instanceof categoryinterface)) { throw new \invalidargumentexception( sprintf( 'invalid argument provided for "$entity" expected %s, received %s ', categoryinterface class, get debug type($entity), ), ); } return !in array( needle (int)$entity >getid(), haystack $this >excludedcategories, strict true, ); } } see entities is indexable determiner https //docs klevu com/klevu magento v4/entities is indexable determiner for more information change visibility in the synced data to change the data synced we must override the pipeline yaml for categories create the override yaml file vendor/module/etc/pipeline/add update yml stages iterateindexingrecordsbatch stages iterateindexingrecords stages processcategory stages createrecord stages attributes stages visibility vendor module etc/pipeline/category/attributes/visibility yml create the imported visibility yaml file it is possible to do this all in one yaml file, but separating it into multiple files makes it easier to read to remove all categories from search vendor/module/etc/pipeline/category/attributes/visibility yml stages \ pipeline stage\staticvalue args value \[""] to remove some categories from search take the current category and pass it into a transformer this transformer should return an array of strings containing with or search vendor/module/etc/pipeline/category/attributes/visibility yml stages \ pipeline stage\extract args extraction currentcategory transformations \ tovisibility create the transformer to handle the visibility vendor/module/pipeline/transformer/tovisibility php namespace vendor\module\pipeline\transformer; use klevu\pipelines\transformer\transformerinterface; use magento\catalog\api\data\categoryinterface; class tovisibility implements transformerinterface { / @return string\[] / public function transform( mixed $data, ?argumentiterator $arguments = null, ?\arrayaccess $context = null, ) array { if (!($data instanceof categoryinterface)) { throw new invalidinputdataexception( transformername $this class, expectedtype categoryinterface class, arguments $arguments, data $data, ); } return $this >visibilityprovider >get( category data, ); } if you create a transformer in a custom module it must be registered with the pipelines before it can be used see registering transformers https //docs klevu com/klevu magento v4/entities registering transformers for more information alternatively we could add an attribute to the category for visibility and use that instead of a transformer e g vendor/module/etc/pipeline/category/attributes/visibility yml stages \ pipeline stage\extract args extraction currentcategory getvisibility() assumes getvisibility would return either or search if it returned an integer then we'd have to map that see the visibility yaml for products module m2 indexing products/etc/pipeline/product/attributes/visibility yml see pipeline customisation https //docs klevu com/klevu magento v4/entities pipelines customisation for more information on how to customise pipelines