How To
Add-To-Cart Function
9min
the actual method to add items to the cart/checkout workflow varies by platform this guide will show you where to locate the klevu add to cart function if you should need to make adjustments, or write your own functionality general use the klevu add to cart button calls the function klevu addtocart / id product id url product page url qty desired quantity (default 1) / function klevu addtocart( id, url, qty ) { // add to cart logic here } example simply implement the function klevu addtocart with your own logic of what you would like to happen when a customer clicks on add to cart enabling the button and/or changing the button text can be done using variables as presented below \<script type="text/javascript"> klevu addtocartenabled = true; // or false to disable klevu uc useroptions = { 	 addtocartbutton 'add to bag!' // custom button text } 	 function klevu addtocart( id, url, qty ) { // add your logic here! } \</script> klevu recommendations the klevu recommendations add to cart function call contains an extra argument recskey to identify the specific recommendations banner from where the click originated \<script type="text/javascript"> function klevu addtocart( id, url, qty, recskey ) { if ( recskey == 'abc 123 xyz 321' ) { // add a specific recommendation logic here! } else { // add your logic here! } } \</script> supported klevu apps / extensions if you are already using klevu search via a klevu app/extension, you may find that the add to cart button within the klevu templates or recommendations is already set and operational there should be no need to further modify these functions they are included below for reference shopify configure in kmc smart search → customizations → add to cart function location snippets/klevu add to cart \<script type="text/javascript"> if ('undefined' === typeof klevu addtocart) { function klevu addtocart( id, url, qty ) { if ('undefined' !== typeof klevu customaddtocart) { klevu customaddtocart(id, url, 1); } else { var urlprotocol = ( "https " === document location protocol ? "https //" "http //" ); var url = urlprotocol + window\ location hostname + '/cart/add?id=' + id + '\&quantity=1'; window\ location assign(url); } } } \</script> note you can use the provided check for klevu customaddtocart() ( line 5 above ) for custom cart functionality bigcommerce configure in kmc smart search → customizations → add to cart function location templates/components/klevu/klevu add to cart html \<script type="text/javascript"> function klevu addtocart( id, url, qty ) { if( 'undefined' !== typeof klevu customaddtocart ){ klevu customaddtocart( id, url, qty ); } else{ var urlprotocol = ( "https " === document location protocol ? "https //" "http //" ); var url = urlprotocol + window\ location hostname +'/cart php?action=add\&product id='+id; window\ location assign(url); } } \</script> note you can use the provided check for klevu customaddtocart() ( line 4 above ) for custom cart functionality magento configure in kmc smart search → customizations → add to cart enable in magento admin stores → configuration → klevu → search configuration → add to cart button function location vendor/klevu/module addtocart/view/frontend/templates/klevu/addtocart/index phtml \<?php if ($block >isaddtocartenabled()) ?> \<script type="text/javascript"> function klevu addtocart(id, url, qty) { var el = document createelement("a"); el setattribute( "data post", '{' + '"action" "\<?= $block >escapeurl($block >geturl('checkout/cart/add')) ?>",' + '"data" {"product" "' + id + '", "qty" "' + qty + '"}' + '}' ); el style width = "0px !important"; el style height = "0px !important"; document body appendchild(el); el click(); document body removechild(el); } \</script> \<?php endif; ?> if modifications are required, it is strongly recommended to properly extend the klevu module to avoid potential conflicts during future updates to the system salesforce commerce cloud configure in kmc smart search → customizations → add to cart \<script type="text/javascript"> function klevu addtocart( id, url, qty ) { $ spinner() start(); var addtocarturl = 'https //development web demandware net/on/demandware store/sites xx site/default/cart addproduct'; var form = { pid id, quantity 1 }; $ ajax({ url addtocarturl, method 'post', data form, success function (data) { $ spinner() stop(); }, error function () { $ spinner() stop(); window\ location replace(url); } }); } \</script>