The add-to-cart function allows your customers to add a product and/or an item (SKU) to the cart directly from the search layer and potentially from product recommendation slots.
This requires up to three functions that enable:
- Interaction with an add-to-cart function on your website, provided by you.
- Reading the cart contents through a datalayer or equivalent.
- Optionally, a function or URL that allows the search layer to open the cart when the user clicks on the cart icon that will be added to the search layer.
Product tiles can thus display an add-to-cart button. When your customers click on this button, the add-to-cart function you have provided is executed.
Add to cart function
This function must meet the following specifications.
Signature
- function(<id_article>, <quantity>, <loose>) : <code>
- function(<id_article>, <quantity>, <loose>) : <Promise<code>>
You provide a synchronous or asynchronous function that will perform the actual add-to-cart operation on your platform. If your function is asynchronous, the return code must be transmitted via a "promise."
Note that Sensefuel can add more the arguments of the function to meet requirements of your JavaScript function. The arguments can be dynamically populated with data from your attributes (from your catalog or "offer" local inventory feed) for instance.
Return codes
Return codes from your function or through the "promise" of your function:
- "success": successful add to cart
- "error": the action is not correct and the add to cart action could not be executed
- "information required": your customer must perform an action (such as logging in, entering a zip code, etc.)
Example (JavaScript)
async function addToCart(id, quantity, loose) {
try {
await fetch("/php_process/cart_engine.php", {
method: "POST",
body: {
id, quantity, loose
}
});
window.sensefuelDatalayer.articles[id] = { quantity, loose };
return "success";
} catch(err) {
return "error";
}
}
Availability
The function must be available throughout your entire website.
Behavior
If the function's return code is “information required”, the search layer may close automatically so that you can display a modal/pop-in for your customers to fill in the required information.
Function name & configuration
To activate or configure this feature, please contact your Customer Success Manager.
Retrieving cart contents
When the add-to-cart function is activated, Sensefuel will retrieve the contents (list of products in the cart) of your cart through a function / variable / datalayer that you provide.
Sensefuel objective is here to manage correctly the display of products that have already been added to the shopping cart as well as providing the count of products in the cart.
The information is only read by Sensefuel, and no data will be altered or stored. Any alteration of the data must be managed on your side.
Signature
- <data>
- function() : <data>
- function() : <Promise<data>>
You provide a synchronous or asynchronous function that returns the data (via a “promise” in the case of an asynchronous function), or a variable (global variable, datalayer, local storage, cookie) that contains this data.
Parameters
- <id_article> (String) : sku identifier (id) of your catalog feed sent to Sensefuel.
- <quantity> (Number) : corresponds to the quantity added to the cart.
- <loose> (Boolean) : indicates whether the quantity is for loose products.
ℹ The add-to-cart data structure can be merged with the other data structures (add-to-list, store id/context, anonymized customer data) into a single data structure if required. To avoid a heavy object, you should delete items whose quantity = 0.
Data Structure
{
articles : {
<id>: {
quantity: <number>,
loose: <boolean>
}
}
}
Availability
The function must be available throughout your entire website.
Function name & configuration
To activate or configure this feature, please contact your Customer Success Manager.
Return code example
{
articles:{
97 : {quantity: 3, loose: false}
1217 : {quantity: 1, loose: false}
1451 : {quantity: 1, loose: false}
7899 : {quantity: 1, loose: false}
}
}
Opening the shopping cart from the Sensefuel search layer
It is possible and recommended that the search layer displays a shopping cart icon that your customer can click on to access the shopping cart.
This cart icon/button either executes a function on click or redirects to a specific URL provided by you. If you use a function, it must meet the following specifications.
Signature
- <url>
- function() : undefined
ℹ You provide a synchronous or asynchronous function that opens the shopping cart. Alternatively, Sensefuel can also use a URL that redirects your customers when they click on the shopping cart icon/button.
Availability
The function must be available throughout your entire website.
Behavior
When the function is executed, the search layer will be closed automatically so that your website can display the shopping cart to your customers on the current page or redirect them on the cart page.
Function name & configuration
To activate or configure this feature, please contact your Customer Success Manager.