Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MiraiTunga/c8b58b5f82d400cf754f623b7bd0f9b0 to your computer and use it in GitHub Desktop.
Save MiraiTunga/c8b58b5f82d400cf754f623b7bd0f9b0 to your computer and use it in GitHub Desktop.
Big Commerce Store Cart Data In Local Storage Then Push to gtm at later stage
//Added script to order-confrirmtion.html page
//store data after payment has been actioned
$("body").on("click","#checkout-payment-continue" ,function () {
var CratItems = {{{json cart.items}}};
var Cart = {{{json cart}}};
StoreCartItems(CratItems);
//console.log(localStorage.getItem('transactionItems'));
StoreGeneralCartInfo(Cart);
//console.log(localStorage.getItem('transactionTotal'));
//console.log(localStorage.getItem('transactionTax'));
//console.log(localStorage.getItem('transactionShipping'));
});
function StoreCartItems(items){
var TransactionItems = [];
for (var i = 0; i < items.length; i++) {
var item = items[i];
var SKU = item["sku"];
var Name = item["name"];
var Quantity = item["quantity"];
var Price = item["price"].value;
var Category = "";
var ItemToStore =
{
'sku':SKU,
'name': Name,
'category': Category,
'price': Price ,
'quantity': Quantity
};
TransactionItems.push(ItemToStore);
}
localStorage.setItem('transactionItems', JSON.stringify(TransactionItems));
}
//Added to chcekout.html page
PushGTMCartData();
function PushGTMCartData(){
var orderid = {{{json checkout.order.id}}};
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'transactionId': orderid,
'transactionAffiliation': '',
'transactionTotal': parseFloat(localStorage.getItem('transactionTotal')),
'transactionTax': parseFloat(localStorage.getItem('transactionTax')),
'transactionShipping': parseFloat(localStorage.getItem('transactionShipping')),
'transactionProducts': JSON.parse(localStorage.getItem('transactionItems'))
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment