Magento B2B MSI
The current version of Klevu’s Magento extension does not natively support indexing of Magento's Multi Source Inventory (Inventory Management) values. The values must be provided into the Klevu index so that they may be used for constraining the results to the desired source availability and/or indicating inventory.
Two parameters are required at the product level during indexing:
- One to identify the item is sold at the selected location
- One to identify the item is in stock at the selected location
On the website front-end, a filter is applied “behind the scenes” to the outgoing search query to constrain results to the visitors selected source/store.
If you are looking for MSI Salable Availability (true/false) only, please review our sub module add-on to see if it useful for your scenario. https://github.com/klevu/msi
The solution below will explain:
1] Options to provide the availability data and stock status at the product level to the Klevu Index:
- Extend the Klevu Search Module
- Klevu Feed XML
- Klevu Indexing API
2] Options to modify the outgoing search query to filter by the selected source and/or stock status:
- Klevu Search API
- Template JS V2 override
- Template JS V1 variable
If you are using the “Preserve your Magento layout” option of the Klevu Magento Module, please contact Customer Support for guidance specific to your version.
The option selected for Indexing and Query will apply globally. It is recommended to NOT mix Indexing methods.
Note: the methods for obtaining the availability data will be the responsibility of your development team.
Once the availability values have been indexed, the outgoing search queries to Klevu can use the values for filtering based on source and/or stock availability.
Note: the name and value pairs used in this document are for examples only. The actual values can be whatever best fits the implementation and business requirements so long as they are consistent.
For Example:
Options to provide the availability data and stock status at the product level to the Klevu Index:
- Extend the Klevu Search Module
- Klevu Feed XML
- Klevu Indexing API
Extend Klevu's Magento module to include the availability datapoints at the product level during catalog indexing.
The Klevu module contains a function designed for adding additional datapoints. This function can be extended as preference and used for additional parameters added at the product level during indexing.
vendor/klevu/module-productsearch/Model/Product/LoadAttribute.php -> processProductAfter()
Source reference : Klevu productsearch on github
Example extended class and logic:
Because there are many ways to work within the Magento framework, the actual implementation as well as methods and applied logic for the availability data will be the responsibility of your development team.
It is strongly recommended to properly extend the Klevu extension for this purpose to avoid potential conflicts during future updates to the system.
1] Extend the LoadAttribute class as preference.
2] Override the processProductAfter function within the LoadAttribute class.
This example adds source_availability and source_instock as facets to the Klevu module's Indexing API call to Klevu. The product is sold at Location1 and Location2 (comma separated Array), but is only in stock at Location1.
API reference : Data Indexing
You may disengage the Klevu module's built-in sync and provide an external XML feed file with the full catalog data, including the additional Availability datapoints.
Please reference the Knowledge Base article for more information on using XML for data indexing.
The source availability datapoints can be entered using the <attribute> node.
In this example the product is sold at Location1 and Location2, but is only in stock at Location1.
Note: <is_facet> is set to true to allow filtering on the attribute.
Source References : Klevu Feed Format Guide and Klevu Feed Example XML
You may disengage the Klevu module's built-in sync and use Klevu’s Data Indexing directly for indexing the full catalog data, including the additional Availability datapoints.
The source availability datapoints can be sent using the other key:value pair (as facets)
In this example the product is sold at Location1 and Location2, but is only in stock at Location1.
API reference : Data Indexing
Options to modify the outgoing search query to filter by the selected source and/or stock status:
- Klevu Search API
- Template JS V2 override
- Template JS V1 (Legacy)
If you are using the “Preserve your Magento layout” option of the Klevu Magento Module, please contact Customer Support for guidance specific to your version.
Using the API parameter customANDQuery, a constraint can be applied for the query to those items that correspond to the selected source_availability value.
Example API request JSON using customANDQuery constraint
This example adds source_availability and source_instock as filters to the Template JS API call to Klevu. The web visitor has selected to see items sold at Location1 and only in stock at Location1.
Source Reference :
To apply the Availability constraint within the context of the Klevu Template JS UI the outgoing search request needs to be intercepted and modified.
See : Custom Overrides for details on preparing for Klevu JS overriding
You may also find Klevus Example Customization Module on giHub useful for this scenario (and more).
Example override using customANDQuery constraint
This example adds source_availability and source_instock as constraints to the Template JS API call to Klevu. The web visitor has selected to see items sold at Location1 and only in stock at Location1.
The result set will only include items that meet these requirements.
Note: The above example applies the constraint to productList query only. For a complete Javascript example of applying this to all query, see the Complete Template JS Example below.
Complete Template JS Example
This example uses a common function to apply the availability constraint via customANDQuery override for all queries on Quicksearch, Search Landing, and Category pages.
Klevus legacy template provides a basic means to apply preset filters via a JavaScript variable.
This example adds source_availability and source_instock as filters to the Template JS API call to Klevu. The web visitor has selected to see items sold at Location1 and only in stock at Location1.