window.addEventListener('DOMContentLoaded', function() {
var ClydeClientKey = "ck_live_vXFW8b62TkHD6aKw"; //[!!! REPLACE THIS CLYDE API KEY !!!]
var productsArray = [];
const cartItems = {};
$.getJSON('/cart.js').then((data)=>{
for (let item of data.items) {
cartItems[item.variant_id] = {
title:item.title,
properties:item.properties,
quantity:item.quantity,
key:item.key
}
}
});
//Load the Clyde.init() function
//Define any required parameters and callback functions
var initInterval = setInterval(function(){
if(document.body){
clearInterval(initInterval);
Clyde.init(
{
key: ClydeClientKey, //your 'live' Client Key listed in Clyde Dashboard settings
skuList: productsArray,
},
function () {
//Cart CTA logic
//Custom function defined to loop through each Product SKU in Cart and load Cart CTA
if(Clyde.getSettings().cart == true){//Recommended logic which allows you to enable/disable Cart CTA via Clyde Dashboard settings
loadClydeCartCTAs(productsArray);
}
});
}
}, 100);
//Function defined to loop through your Cart items and append Cart CTA for each
//Note that you can add this logic to any existing function that builds your Cart page / Mini Cart
function loadClydeCartCTAs(productsInCartArray) {
productsInCartArray.forEach(function (sku) {
Clyde.appendToSelectorWithSku(sku, `#clyde-${sku}`, closeModalCb);
});
}
//Function defined to handle whether or not to add a Contract SKU to cart based on the shoppers interaction with the Modal
//Note that clicking "I dont need to protect my purchase" or closing the modal via the "X" or ESC key will result in Clyde.getSelectedContract == null
function closeModalCb() {
var selectedContract = Clyde.getSelectedContract();
if (selectedContract) {
//Add any logic/functions needed to add the Contract SKU to cart
//You can simply pass the Contract SKU to any existing "add to cart" function
//This will vary based on each eCommerce system
addClydeToCart(selectedContract);
}
}
//Function to add Clyde Contract to Cart
function addClydeToCart(selectedContract){
if(document.querySelector("quantitySelector")!=null){
productQuantity = document.querySelector(quantitySelector).value;
}
else{
productQuantity = 1;
}
var productName;
var activeVariantId = Clyde.getActiveProduct().sku;
if(meta.product!=null){
meta.product.variants.forEach(function(variant){
if (variant.id == activeVariantId) {
productName = variant.name;
}
});
}
else{
productName = cartItems[selectedContract.reference].title;
productQuantity = cartItems[selectedContract.reference].quantity;
}
setTimeout(function(){
let formData = {
'items': [{
quantity: productQuantity,
id: selectedContract.shopifyVariantId,
properties: {
Reference: selectedContract.reference,
Contract: selectedContract.contractHash, // This is a hashed numeric ID of a contract in our system, in case the contract is eventually deleted from Shopify.
Product: productName// This is NOT required, nor returned on the contract, but lets the customer see which contract goes with which product in their cart.
}
}]
};
fetch('/cart/add.js', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(formData)
})
.then(response => {
setTimeout(function(){
window.location.reload();
}, 500);
return response.json();
})
.catch((error) => {
console.error('Error:', error);
});
}, 500);
}
///
var addToCartSelector = ".button-atc";
//Define the active Product SKU on PDP
var productData = {
sku: "",
price: "",
};
var interval = setInterval(function(){
if(Clyde.checkReady()==true){ //Ensure Clyde scripts are ready
ClydePDP();
clearInterval(interval);
}
}, 100);
function ClydePDP() {
//PDP CTA logic
if(Clyde.getSettings().productPage == true){//Recommended logic which allows you to enable/disable PDP CTA via Clyde Dashboard settings
Clyde.setActiveProduct(productData,function(){
Clyde.appendToSelector("#clyde-cta");//Any ID or Class for an element where you'd like to inject the Clyde PDP CTA. We recommend creating an empty div specific to Clyde
});
//Add an event listener to any "Add To Cart" buttons to use the Clyde.getSelectedContract logic and add Contract SKUs to Cart
//Note: you may want to add the Clyde.getSelectedContract logic to an existing "Add To Cart" event or combine with Modal CTA if you'd prefer
document
.querySelector(addToCartSelector)
.addEventListener("click", function () {
if (Clyde.checkInit() && Clyde.checkReady()) {
var selectedContract = Clyde.getSelectedContract();
if (selectedContract) {
//Add contract to Cart as part of your existing "Add to Cart" function along with the Product SKU
addClydeToCart(selectedContract);
}
}
});
}
//Modal CTA on Add To Cart event
if (Clyde.getSettings().modal == true) {//Recommended logic which allows you to enable/disable Modal via Clyde Dashboard settings
if(Clyde.getActiveProduct()==null){
Clyde.setActiveProduct(productData,function(){});
}
//Add an event listener to your Add To Cart buttons to fire the Clyde Modal if the Product is eligible
//Note: you may want to simply add this Clyde logic to any existing Add To Cart events
document
.querySelector(addToCartSelector)
.addEventListener("click", function () {
if(Clyde.getSelectedContract() == null){
Clyde.showModal(null, closeModalCb); //1st parameter is optional "on open modal callback". 2nd parameter is required "on close modal callback"
}
});
}
//Ensure correct variant is set as the active product
if(Clyde.getSettings().productPage == true || Clyde.getSettings().modal == true){
var href = window.location.href; // Store the current page URL on load
setInterval(function() { // listen for changes
if (href != location.href)
{
href = location.href; // page has changed, set new page as 'current'
var variantId = new URL(href).searchParams.get('variant');
productData = {
sku: variantId,
price: "",
};
Clyde.setActiveProduct(productData);
}
}, 500);
}
}
});