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 \<?= $securerenderer >rendertag( tagname 'script', attributes \['type' => 'text/javascript', 'id' => 'klevu init'], content "\nif(typeof klevusettings !== 'undefined'){klevu(klevusettings);}\n", textcontent false, ) ?> \<?php / inject javascript into klevu frontend init after to make changes after klevu() is called / ?> \<?= $block >getchildhtml('klevu frontend init after') ?> adding code via theme magento theme structure documentation in your theme create a module klevu frontend, app/code/design/frontend/vendor/theme/klevu frontend 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 \<?xml version="1 0"?> \<page xmlns\ xsi="http //www w3 org/2001/xmlschema instance" xsi\ nonamespaceschemalocation="urn\ magento\ framework\ view/layout/etc/page configuration xsd"> \<body> \<referenceblock name="klevu frontend init after"> \<block name="vendor theme klevu init after" template="klevu frontend klevu/init after phtml" before=" "/> \</referenceblock> \</body> \</page> 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 \<?php $scriptstring = " // add any customisation here "; ?> \<?= $securerenderer >rendertag( tagname 'script', attributes \['type' => 'text/javascript'], content $scriptstring, textcontent false ) ?> adding code via a custom module note in the example below we have used module vendor module magento module creation documentation create a custom module vendor module , app/code/vendor/module with the following directory structure note full module creation is not shown here app \ code \ vendor \ module \ view \ layout \ templates 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 \<?xml version="1 0"?> \<page xmlns\ xsi="http //www w3 org/2001/xmlschema instance" xsi\ nonamespaceschemalocation="urn\ magento\ framework\ view/layout/etc/page configuration xsd"> \<body> \<referenceblock name="klevu frontend init after"> \<block name="vendor module klevu init after" template="vendor module klevu/init after phtml" before=" "/> \</referenceblock> \</body> \</page> 3\ create the template file that will contain your customisations vendor/module/view/frontend/templates/klevu/init after phtml \<?php $scriptstring = " // add any customisation here "; ?> \<?= $securerenderer >rendertag( tagname 'script', attributes \['type' => 'text/javascript'], content $scriptstring, textcontent false ) ?>