Files
wyczarujprezent.pl/modules/ecsgtmpro/views/js/dl.js
2024-10-28 22:14:22 +01:00

141 lines
5.1 KiB
JavaScript

/*
* 2022 ECSoft
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to dev.ecsoft@gmail.com so we can send you a copy immediately.
*
*
* @author ECSoft <dev.ecsoft@gmail.com>
* @copyright 2022 ECSoft
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of ECSoft
*/
var ecsGtmPro = {
addToCart: function (product_id, attribute_id, qty, addedFromProductPage, callerElement) {
if (product_id) {
ecsGtmPro.pushProduct('cart-add', product_id, attribute_id, qty);
}
},
removeFromCart: function (product_id, attribute_id, qty) {
if (product_id) {
ecsGtmPro.pushProduct('cart-remove', product_id, attribute_id, qty);
}
},
productClick: function (product_id, attribute_id) {
if (product_id) {
ecsGtmPro.pushProduct('product-click', product_id, attribute_id);
}
},
pushProduct: function (action, product_id, attribute_id, qty) {
attribute_id = attribute_id || 0;
qty = qty || 1;
var ecsGtmProAjax = new XMLHttpRequest();
ecsGtmProAjax.onreadystatechange = function() {
if (ecsGtmProAjax.readyState == 4) {
if (ecsGtmProAjax.status == 200) {
var datalayerJs = ecsGtmProAjax.responseText;
try {
let datalayerCartAction = JSON.parse(datalayerJs);
dataLayer = dataLayer || [];
dataLayer.push(datalayerCartAction);
} catch(e) {
console.log("[ECSGTMPRO] error parsing json");
}
}
}
};
ecsGtmProAjax.open("GET", ecsGtmAjaxGetProductUrl + "?action=" + action + "&id=" + product_id + "&id_attribute=" + attribute_id + "&qty=" + qty, true);
ecsGtmProAjax.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
ecsGtmProAjax.send();
},
handleValidateShippingStep: function ($submitBtn, event, ecsGtmTriggered, shippingName) {
if(typeof ecsGtmTriggered === 'undefined' || !ecsGtmTriggered) {
event.preventDefault();
try {
dataLayer = dataLayer || [];
let cloneDatalayer = JSON.parse(JSON.stringify(ecsGtmProDatalayer))
cloneDatalayer.event = 'add_shipping_info';
cloneDatalayer.ecommerce.shipping_tier = shippingName;
dataLayer.push(cloneDatalayer);
} catch (e) {
console.error(e);
}
setTimeout(function() {
$submitBtn.trigger('click', [1])
}, 500);
}
}
};
if (typeof(ajaxCart) != 'undefined') {
var ajaxCartAddFunc = ajaxCart.add;
ajaxCart.add = function(idProduct, idCombination, addedFromProductPage, callerElement, quantity, wishlist) {
ajaxCartAddFunc(idProduct, idCombination, addedFromProductPage, callerElement, quantity, wishlist);
ecsGtmPro.addToCart(idProduct, idCombination, quantity, addedFromProductPage, callerElement);
};
var ajaxCartRemoveFunc = ajaxCart.remove;
ajaxCart.remove = function(idProduct, idCombination, customizationId, idAddressDelivery) {
ajaxCartRemoveFunc(idProduct, idCombination, customizationId, idAddressDelivery);
ecsGtmPro.removeFromCart(idProduct, idCombination);
};
}
var deleteProductFromSummary = (function(id) {
var original_deleteProductFromSummary = deleteProductFromSummary;
return function(id) {
var productId = 0;
var productAttributeId = 0;
var ids = 0;
ids = id.split('_');
productId = parseInt(ids[0]);
if (typeof(ids[1]) !== 'undefined') {
productAttributeId = parseInt(ids[1]);
}
var cart_qty = parseInt($('input[name=quantity_' + id + ']').val());
ecsGtmPro.removeFromCart(productId, productAttributeId, cart_qty);
original_deleteProductFromSummary(id);
};
})();
var downQuantity = (function(id, qty) {
var original_downQuantity = downQuantity;
return function(id, qty) {
var productId = 0;
var productAttributeId = 0;
var ids = 0;
ids = id.split('_');
productId = parseInt(ids[0]);
if (typeof(ids[1]) !== 'undefined') {
productAttributeId = parseInt(ids[1]);
}
var val = $('input[name=quantity_' + id + ']').val();
var newVal = val;
if(typeof(qty) == 'undefined' || !qty) {
new_qty = 1;
newVal = val - 1;
} else if (qty < 0) {
new_qty = -qty;
}
if(newVal > 0) {
ecsGtmPro.removeFromCart(productId, productAttributeId, new_qty);
}
original_downQuantity(id, qty);
};
})();