Registering Attributes that are not Magento Attributes
There may be cases where you need to index some entity data to Klevu where a Magento attribute does not represent that data.
If the data needs to be searchable and/or filterable then creating a static attribute is the recommended way to handle that. However, if the data is only to be used on the frontend, then sending it in additionalDataToReturn is a simpler approach and a static attribute would not be required.
e.g. For a product you want to send the following data in the entity payload
{
"name": "Product Name",
"sku": "SKU-101",
"some_attribute": "This is not a Magento attribute, but some third-party data"
}Where some_attribute is not a Magento attribute but is data pulled from some third-party source. With the Klevu JSON API, we must register some_attribute as an attribute before sending data for that attribute in the entity payload.
Klevu provides StaticAttributeProvider to accomplish this. Called “static” as they are hardcoded in the di.xml and cannot be updated via the admin or the database.
We add attributes to StaticAttributeProvider by injecting the required attribute(s) into the attributes argument via di.xml. e.g.
<type name="Klevu\IndexingProducts\Service\Provider\StaticAttributeProvider">
<arguments>
<argument name="attributes" xsi:type="array">
<item name="some_attribute" xsi:type="array">
<item name="attribute_id" xsi:type="number">100001</item> <!-- required -->
<item name="attribute_code" xsi:type="string">some_attribute</item> <!-- required -->
<item name="is_searchable" xsi:type="boolean">true</item> <!-- optional: default = false -->
<item name="is_filterable" xsi:type="boolean">false</item> <!-- optional: default = false -->
<item name="is_returnable" xsi:type="boolean">true</item> <!-- optional: default = false -->
<item name="default_label" xsi:type="string">Some Attribute</item> <!-- optional: default = attribute_code with underscores removed and uppercase first letter -->
<item name="labels" xsi:type="array"> <!-- optional: item names are store ids -->
<item name="1" xsi:type="string">Store Label</item>
<item name="2" xsi:type="string">Étiquette du magasin</item>
<item name="3" xsi:type="string">Store-Etikett</item>
</item>
<!-- The following are used to determine what attribute type is created in Klevu -->
<!-- Klevu attribute types are STRING, NUMBER, MULTIVALUE -->
<!-- If none of these are set the default attribute type is STRING -->
<item name="frontend_input" xsi:type="string">select</item> <!-- optional -->
<item name="backend_type" xsi:type="string">int</item> <!-- optional -->
<item name="source_model" xsi:type="string">Vendor\Module\Model\Source\SomeAttributeSource</item> <!-- optional -->
<item name="is_html_allowed_on_front" xsi:type="number">1</item> <!-- optional -->
<item name="is_global" xsi:type="number">1</item> <!-- optional -->
</item>
</argument>
</arguments>
</type>We can do the same for Categories injecting into Klevu\IndexingCategories\Service\Provider\StaticAttributeProvider