Frontend
Theme JS

Adding Code After Klevu()

7min

There are 2 ways to achieve this:

  • Set the value via your theme. Use this if the value;
    • is different depending on the theme (you use multiple themes in your store).
    • you only have one theme.
  • Set the value via a custom module. Use this if the value;
    • depends on values stored in your database.
    • depends on more complex business logic.
    • is the same across all your themes and you have a lot of themes that would require an update.

How Code is added after Klevu(klevuSettings)

Klevu(klevuSettings) is added to your page head via layout xml here module-m2-frontend/view/frontend/layout/default_head_blocks.xml. Specifically in block Klevu_Frontend.js_settings which adds 2 empty contain blocks, so you can inject your own code before and after klevu(klevuSettings).

The template that generates the settings is module-m2-frontend/view/frontend/templates/html/head/js_settings.phtml.

module-m2-frontend/view/frontend/templates/html/head/js_settings.phtml



Adding Code Via Theme

  1. In your theme create a module Klevu_Frontend, app/code/design/frontend/Vendor/theme/Klevu_Frontend
  2. Create a layout file to add your blocks to our contain klevu_frontend.init_after app/code/design/frontend/Vendor/theme/Klevu_Frontend/layout/default_head_blocks.xml
app/code/design/frontend/Vendor/theme/Klevu_Frontend/layout/default_head_blocks.xml


3. Create the template file that will contain your customisations app/code/design/frontend/Vendor/theme/Klevu_Frontend/templates/klevu/init_after.phtml

app/code/design/frontend/Vendor/theme/Klevu_Frontend/templates/klevu/init_after.phtml



Adding Code Via A Custom Module

Note: in the example below we have used Module Vendor_Module. Magento module creation documentation.

  1. Create a custom module Vendor_Module, app/code/Vendor/Module with the following directory structure. Note full module creation is not shown here.
Text


2. Create a layout file to add your blocks to our contain klevu_frontend.init_after

Vendor/Module/view/frontend/layout/default_head_blocks.xml


3. Create the template file that will contain your customisations

Vendor/Module/view/frontend/templates/klevu/init_after.phtml