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.

JS




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.

JS




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

JS


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

JS


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 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

JS