/**
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 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:
* https://opensource.org/licenses/AFL-3.0
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to a newer
* versions in the future. If you wish to customize this module for your needs
* please refer to CustomizationPolicy.txt file inside our module for more information.
*
* @author Webkul IN
* @copyright Since 2010 Webkul
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

// Consent Mode v2 Setup
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
if (typeof wk_gtm_consent_banner != 'undefined'){
    gtag('consent', 'default', {
        'ad_storage': 'granted',
        'analytics_storage': 'granted',
        'ad_user_data': 'granted',
        'ad_personalization': 'granted',
        'wait_for_update': 500
    });
} else {
    gtag('consent', 'default', {
        'ad_storage': 'denied',
        'analytics_storage': 'denied',
        'ad_user_data': 'denied',
        'ad_personalization': 'denied',
        'wait_for_update': 500
    });
}

(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);

})(window,document,'script','dataLayer',googletagID);
window.dataLayer = window.dataLayer || [];
if(typeof trackingID != 'undefined'){
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', trackingID);
}

$(document).ready(function(){
    if (!localStorage.getItem('wk_cookie_consent')) {
        $('#consent-banner').removeClass('wkd-none');
    }

    $(document).on('click', '#consent-accept', function () {
        gtag('consent', 'update', {
            'ad_storage': 'granted',
            'analytics_storage': 'granted',
            'ad_user_data': 'granted',
            'ad_personalization': 'granted'
        });
        localStorage.setItem('wk_cookie_consent', 'accepted');
        $('#consent-banner').addClass('wkd-none');
    });

    $(document).on('click', '#consent-reject', function () {
        gtag('consent', 'update', {
            'ad_storage': 'denied',
            'analytics_storage': 'denied',
            'ad_user_data': 'denied',
            'ad_personalization': 'denied'
        });
        localStorage.setItem('wk_cookie_consent', 'rejected');
        $('#consent-banner').addClass('wkd-none');
    });

    wk_new_cart_qty = 1;
    if (search_product_event_name) {
        $(document).ajaxComplete(function(event, xhr, settings) {
            if (typeof settings.data !== 'undefined') {
                var ajax_data = settings.data;
                var urlParams = new URLSearchParams(ajax_data);
                var ajax_url = settings.url;
                var search_url = ajax_url.match('/search');
                var search_data = ajax_data.startsWith('s=');
                if (search_data && (typeof search_url !== 'undefined') && (search_url !== null)) {
                    search_term = urlParams.get('s');
                    if ((typeof search_term !== 'undefined') && (search_term !== null)) {
                        dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
                        dataLayer.push({
                            event: "search",
                            ecommerce: {
                                'search_term': search_term
                            }
                        });
                    }
                } else {
                    var add_to_cart_url = ajax_data.match('action=update');
                    if (add_to_cart_event_name && (typeof add_to_cart_url !== 'undefined') && (add_to_cart_url !== null)) {
                        quantity = getParameterValues('qty', ajax_data);
                        wk_new_cart_qty = quantity;
                    }    
                }
            }
        });
    }

    function getParameterValues(param, ajax_data) {  
        var url = ajax_data.slice(ajax_data.indexOf('?') + 1).split('&');  
        for (var i = 0; i < url.length; i++) {  
            var urlparam = url[i].split('=');  
            if (urlparam[0] == param) {  
                return urlparam[1];  
            }  
        }  
    }

    setTimeout(function(){
        $('.__TAG_ASSISTANT_BADGE').hide();
    },500)

    if((controller_name == 'order-confirmation' || controller_name == 'orderconfirmation') && purchase_order_event_name){
        $.ajax({
            url: wk_gtm_controller_link,
            method: 'POST',
            data: {
                ajax: 1,
                id_order: orderID,
                wkStaticToken: wkStaticToken,
                action: 'getDataForOrder'
            },
            success:function(data){
                result = JSON.parse(data);
                products1 = new Array();
                $.each(result['products'],function(i,val){
                    productdata = {
                        item_id: val.product_id,
                        item_name: val.product_name,
                        affiliation: shop_name,
                        coupon: val.coupon,
                        currency: result.currency,
                        discount: val.discount,
                        index: i,
                        item_brand: val.item_brand,
                        item_category: val.category,
                        item_list_id: val.id_category,
                        item_list_name: val.category,
                        item_variant: val.variant,
                        price: parseFloat(val.price),
                        quantity: val.quantity
                    }
                    products1.push(productdata);
                });
                dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
                dataLayer.push({
                    event: "purchase",
                    ecommerce: {
                        transaction_id: result.transaction_id,
                        affiliation: shop_name,
                        value: parseFloat(result.total_cart_price),
                        tax: parseFloat(result.total_tax),
                        shipping: parseFloat(result.shipping_price),
                        currency: result.currency,
                        coupon: result.coupon,
                        items: products1
                    }
                });
            }
        });
    } else if (controller_name == 'product') {
        if (view_product_event_name) {
            dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
            dataLayer.push({
                event: "view_item",
                ecommerce: {
                    items: [
                        {
                            item_id: productID,
                            item_name: productName,
                            affiliation: shop_name,
                            coupon: wkProductCoupon,
                            currency: wkCurrencyCode,
                            discount: wkProductDiscount,
                            index: 0,
                            item_brand: productBrandName,
                            item_category: productCategory,
                            item_list_id: productCategoryId,
                            item_list_name: productCategory,
                            item_variant: productVariant,
                            price: productPrice,
                            quantity: 1
                        }
                    ]
                }
            });
        }
        if (product_share_event_name) {
            $(document).on('click', '.social-sharing li', function () {
                var rawClass = $(this).attr('class').split(' ')[0]; // e.g., "facebook"
                var shareType = rawClass.charAt(0).toUpperCase() + rawClass.slice(1); // Capitalized
                var href = $(this).find('a').attr('href');
                var decodedHref = decodeURIComponent(href);
                var contentType = 'Link'; // default
                if (decodedHref.includes('media=')) {
                    contentType = 'Image';
                } else if (decodedHref.includes('text=')) {
                    contentType = 'Text';
                }
                dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
                dataLayer.push({
                    event: "share",
                    method: shareType,
                    content_type: contentType,
                    item_id: productID,
                });
            });
        }
    } else if (controller_name == 'category' && view_category_event_name && wk_category_products) {
        products1 = new Array();
        $.each(wk_category_products,function(i,val) {
            productdata = {
                item_id: val.product_id,
                item_name: val.product_name,
                affiliation: shop_name,
                coupon: val.coupon,
                currency: wkCurrencyCode,
                discount: val.discount,
                index: i,
                item_brand: val.item_brand,
                item_category: val.category,
                item_list_id: id_category,
                item_list_name: category_name,
                item_variant: val.variant,
                price: parseFloat(val.price),
                quantity: val.quantity
            }
            products1.push(productdata);
        });
        dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
        window.dataLayer.push({
            'Page': 'Category',
            'event': 'view_item_list',
            'ecommerce': {
                item_list_id: id_category,
                item_list_name: category_name,
                items: products1
            },
        });
    } else if (controller_name == 'contact' && contact_form_event_name) {
        $(document).on('click', '[name="submitMessage"]', function () {
            if ($('[name="message"]').val().trim() == '') {
                return;
            }
            var orderID = $('#id-order').val();
            if (orderID) {
                $.ajax({
                    url: wk_gtm_controller_link,
                    method: 'POST',
                    async: false,
                    data: {
                        ajax: 1,
                        id_order: orderID,
                        wkStaticToken: wkStaticToken,
                        action: 'getDataForOrder'
                    },
                    success:function(data) {
                        result = JSON.parse(data);
                        dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
                        dataLayer.push({
                            event: "generate_lead",
                            currency: wkCurrencyCode,
                            value: result.total_cart_price,
                            lead_source: contactFormLable
                        });
                        $('.contact-form form').submit();
                    }
                });
            } else {
                dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
                dataLayer.push({
                    event: "generate_lead",
                    currency: wkCurrencyCode,
                    value: 0,
                    lead_source: contactFormLable,
                });
            }
        });
    } else if (controller_name == 'identity' && customer_update_event_name) {
        $('#customer-form [type="submit"]').click(function() {
            var f_name = $('#field-firstname').val();
            var l_name = $('#field-lastname').val();
            var email = $('#field-email').val();
            if (f_name && l_name && email && id_customer)
            window.dataLayer = window.dataLayer || [];
            window.dataLayer.push({
                'Page': 'My Account',
                'page_type': 'Customer Profile',
                'event': 'profileUpdate',
                'Customer Information': {
                    'Customer ID': id_customer,
                    'First Name': f_name,
                    'Last Name': l_name,
                    'Email': email,
                },
            });
        });
    } else if(controller_name == 'cart' && view_cart_event_name) {
        $.ajax({
            url: wk_gtm_controller_link,
            method: 'POST',
            data: {
                ajax: 1,
                wkStaticToken: wkStaticToken,
                action: 'getAllProductData'
            },
            success:function(data) {
                result = JSON.parse(data);
                products1 = new Array();
                $.each(result['products'],function(i,val) {
                    productdata = {
                        item_id: val.product_id,
                        item_name: val.product_name,
                        affiliation: shop_name,
                        coupon: val.coupon,
                        currency: result.currency,
                        discount: val.discount,
                        index: i,
                        item_brand: val.item_brand,
                        item_category: val.category,
                        item_list_id: val.id_category,
                        item_list_name: val.category,
                        item_variant: val.variant,
                        price: parseFloat(val.price),
                        quantity: val.quantity
                    }
                    products1.push(productdata);
                });
                dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
                dataLayer.push({
                    event: "view_cart",
                    ecommerce: {
                        currency: result.currency,
                        value: result.cart_value,
                        items: products1
                    }
                });
            }
        });
    } else if ((typeof wk_listing_products !== 'undefined') && wk_listing_products && gtm_product_listing_event_name && wk_page_name) {
        products1 = new Array();
        $.each(wk_listing_products,function(i,val) {
            productdata = {
                item_id: val.product_id,
                item_name: val.product_name,
                affiliation: shop_name,
                coupon: val.coupon,
                currency: wkCurrencyCode,
                discount: val.discount,
                index: i,
                item_brand: val.item_brand,
                item_category: val.category,
                item_list_id: wk_page_name,
                item_list_name: wk_page_name,
                item_variant: val.variant,
                price: parseFloat(val.price),
                quantity: val.quantity
            }
            products1.push(productdata);
        });
        dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
        window.dataLayer.push({
            'Page': wk_page_name,
            'event': 'view_item_list',
            'ecommerce': {
                item_list_id: wk_page_name,
                item_list_name: wk_page_name,
                items: products1
            },
        });
    }

    $(wk_html_elements.login_button).submit(function() {
        if ($(this).attr('id') == 'login-form' && customer_login_event_name) {
            var email = $('[name="email"').val();
            localStorage.setItem("gtm_email", email);
        }
    });

    if(id_customer && (getCookie("gtm_email") != null && getCookie("gtm_email") != 0)) {
        var wk_gtm_email = getCookie("gtm_email");
        window.dataLayer = window.dataLayer || [];
        $.ajax({
            url: wk_gtm_controller_link,
            data: {
                ajax:1,
                action:'getCustomerDetails',
                wkStaticToken: wkStaticToken,
                email: wk_gtm_email
            },
            success: function(data) {
                result = JSON.parse(data)
                window.dataLayer = window.dataLayer || [];
                window.dataLayer.push({
                    'Page': 'My Account',
                    'page_type': 'Customer',
                    'event': 'customerLogin',
                    'Customer Information': {
                        'Customer ID': result.id_customer,
                        'First Name': result.fname,
                        'Last Name': result.lname,
                        'Email': result.email,
                    },
                });
                localStorage.setItem("gtm_email", 0);
            }
        });
    }

    function getCookie(cname) {
        return localStorage.getItem(cname);
    }

    if (wk_signup_gtm_email && customer_signup_event_name) {
        $.ajax({
            url: wk_gtm_controller_link,
            data: {
                ajax:1,
                action:'getCustomerDetails',
                wkStaticToken: wkStaticToken,
                email: wk_signup_gtm_email
            },
            success: function(data) {
                result = JSON.parse(data)
                window.dataLayer = window.dataLayer || [];
                window.dataLayer.push({
                    'Page': 'My Account',
                    'page_type': 'Customer',
                    'event': 'customerLogin',
                    'Customer Information': {
                        'Customer ID': result.id_customer,
                        'First Name': result.fname,
                        'Last Name': result.lname,
                        'Email': result.email,
                    },
                });
            }
        });
    }

    $(document).on('click', wk_html_elements.begin_checkout, function (e) {
        if(controller_name == 'cart' && begin_checkout_event_name) {
            e.preventDefault();
            var url = $(this).attr('href');
            $.ajax({
                url: wk_gtm_controller_link,
                method: 'POST',
                data: {
                    ajax: 1,
                    wkStaticToken: wkStaticToken,
                    action: 'getAllProductData'
                },
                success:function(data) {
                    result = JSON.parse(data);
                    products1 = new Array();
                    $.each(result['products'],function(i,val) {
                        productdata = {
                            item_id: val.product_id,
                            item_name: val.product_name,
                            affiliation: shop_name,
                            coupon: val.coupon,
                            currency: result.currency,
                            discount: val.discount,
                            index: i,
                            item_brand: val.item_brand,
                            item_category: val.category,
                            item_list_id: val.id_category,
                            item_list_name: val.category,
                            item_variant: val.variant,
                            price: parseFloat(val.price),
                            quantity: val.quantity
                        }
                        products1.push(productdata);
                    });
                    dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
                    dataLayer.push({
                        event: "begin_checkout",
                        ecommerce: {
                            items: products1
                        },
                        'eventCallback': function() {
                            window.location.href = url;
                        }
                    });
                }
            });
        }
    });

    $(document).on('click', wk_html_elements.thumbnail, function (e) {
        if (typeof wk_page_name != undefined && controller_name == 'index') {
            wk_page_name = controller;
        }
        if ((controller_name == 'category' || wk_page_name) && select_product_event_name) {
            e.preventDefault();
            var url = $(this).attr('href');
            var product_id = $(this).parent().closest('.product-miniature').data('id-product');
            var product_attr_id = $(this).parent().closest('.product-miniature').data('id-product-attribute');
            $.ajax({
                url: wk_gtm_controller_link,
                method: 'POST',
                data: {
                    ajax: 1,
                    wkStaticToken: wkStaticToken,
                    action: 'getProductData',
                    id_product: parseInt(product_id),
                    id_product_attribute: parseInt(product_attr_id)
                },
                success:function(data) {
                    result = JSON.parse(data);
                    dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
                    dataLayer.push({
                        event: "select_item",
                        ecommerce: {
                            items: [
                                {
                                    item_id: result.product_id,
                                    item_name: result.product_name,
                                    affiliation: result.shop_name,
                                    coupon: result.coupon,
                                    currency: result.currency,
                                    discount: result.discount,
                                    index: 0,
                                    item_brand: result.item_brand,
                                    item_category: result.category,
                                    item_list_id: result.item_list_id,
                                    item_list_name: category_name,
                                    item_variant: result.variant,
                                    price: result.price,
                                    quantity: result.quantity
                                }
                            ]
                        },
                        'eventCallback': function() {
                            window.location.href = url;
                        }
                    });
                }
            });
        }
    });

    if (wk_opc_enabled) {
        $(document).on('change', '.wk-shipping-carriers [type="radio"]', function () {
            wkGTMShippingInfo();
        });

        $(document).on('change', 'input[name=payment-option]', function () {
            wkGTMPaymentInfo();
        });
    } else {
        $(document).on('click', wk_html_elements.add_shipping, function () {
            wkGTMShippingInfo();
        });

        $(document).on('click', wk_html_elements.add_payment, function () {
            if (controller_name == 'order' && add_payment_info_event_name) {
                var formElement = $(this);
                if (wk_opc_enabled) {
                    var payment_div_id = $('[name="payment-option"]:checked').attr('id');
                    var wk_payment_type = $('.wk-selected-payment-'+payment_div_id+' p').text().trim();
                } else {
                    var payment_div_id = $('[name="payment-option"]:checked').parent().closest('.payment-option').attr('id');
                    var wk_payment_type = $('#'+payment_div_id+' label span').text().trim();
                }
                $.ajax({
                    url: wk_gtm_controller_link,
                    method: 'POST',
                    data: {
                        ajax: 1,
                        wkStaticToken: wkStaticToken,
                        action: 'getAllProductData'
                    },
                    success:function(data) {
                        result = JSON.parse(data);
                        products1 = new Array();
                        $.each(result['products'],function(i,val) {
                            productdata = {
                                item_id: val.product_id,
                                item_name: val.product_name,
                                affiliation: shop_name,
                                coupon: val.coupon,
                                currency: result.currency,
                                discount: val.discount,
                                index: i,
                                item_brand: val.item_brand,
                                item_category: val.category,
                                item_list_id: val.id_category,
                                item_list_name: val.category,
                                item_variant: val.variant,
                                price: parseFloat(val.price),
                                quantity: val.quantity
                            }
                            products1.push(productdata);
                        });
                        dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
                        dataLayer.push({
                            event: "add_payment_info",
                            currency: result.currency,
                            value: result.cart_value,
                            coupon: result.coupon,
                            payment_type: wk_payment_type,
                            ecommerce: {
                                items: products1
                            },
                            'eventCallback': function() {
                                formElement.submit();
                            }
                        });
                    }
                });
            }
        });
    }

    if (typeof prestashop !== 'undefined') {
        if (view_product_event_name) {
            prestashop.on('updatedProduct', function(event){
                var idProduct = $('#product_page_product_id').val();
                var idProductAttribute = event.id_product_attribute;
                $.ajax({
                    url: wk_gtm_controller_link,
                    method: 'POST',
                    data: {
                        ajax: 1,
                        wkStaticToken: wkStaticToken,
                        action: 'getProductData',
                        id_product: parseInt(idProduct),
                        id_product_attribute: parseInt(idProductAttribute)
                    },
                    success:function(data) {
                        result = JSON.parse(data);
                        dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
                        dataLayer.push({
                            event: "view_item",
                            ecommerce: {
                                items: [
                                    {
                                        item_id: result.product_id,
                                        item_name: result.product_name,
                                        affiliation: result.shop_name,
                                        coupon: result.coupon,
                                        currency: result.currency,
                                        discount: result.discount,
                                        index: 0,
                                        item_brand: result.item_brand,
                                        item_category: result.category,
                                        item_list_id: result.item_list_id,
                                        item_list_name: category_name,
                                        item_variant: result.variant,
                                        price: result.price,
                                        quantity: 1
                                    }
                                ]
                            }
                        });
                    }
                });
            });
        }

        $(document).on('click', wk_html_elements.wishlist_icon, function (e) {
            if (add_to_wishlist_event_name && $(this).text() == 'favorite_border') {
                e.preventDefault();
                if ($(this).parent().closest('.product-miniature').length) {
                    var product_id = $(this).parent().closest('.product-miniature').data('id-product');
                    var product_attr_id = $(this).parent().closest('.product-miniature').data('id-product-attribute');
                } else {
                    var product_id = productID;
                    var product_attr_id = idProductAttr;
                }
                if (product_id == 'undefined' || isNaN(product_id)) {
                    return false;
                }
                $.ajax({
                    url: wk_gtm_controller_link,
                    method: 'POST',
                    data: {
                        ajax: 1,
                        wkStaticToken: wkStaticToken,
                        action: 'getProductData',
                        id_product: parseInt(product_id),
                        id_product_attribute: parseInt(product_attr_id)
                    },
                    success:function(data) {
                        result = JSON.parse(data);
                        dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
                        dataLayer.push({
                            event: "add_to_wishlist",
                            currency: result.currency,
                            value: result.price * result.quantity,
                            ecommerce: {
                                items: [
                                    {
                                        item_id: result.product_id,
                                        item_name: result.product_name,
                                        affiliation: result.shop_name,
                                        coupon: result.coupon,
                                        discount: result.discount,
                                        index: 0,
                                        item_brand: result.item_brand,
                                        item_category: result.category,
                                        item_list_id: result.item_list_id,
                                        item_list_name: category_name,
                                        item_variant: result.variant,
                                        price: result.price,
                                        quantity: result.quantity
                                    }
                                ]
                            }
                        });
                    }
                });
            }
        });

        prestashop.on("updateCart", function(event) {
            var response = event.resp;
            if (add_to_cart_event_name
                && (typeof response.id_product !== 'undefined') && (response.id_product > 0)
                && (typeof response.quantity !== 'undefined') && (response.quantity > 0)) {
                if (parseInt(wk_new_cart_qty) > 0) {
                    qty = parseInt(wk_new_cart_qty);
                } else {
                    qty = parseInt(response.quantity)
                }
                $.ajax({
                    url: wk_gtm_controller_link,
                    method: 'POST',
                    data: {
                        ajax: 1,
                        wkStaticToken: wkStaticToken,
                        action: 'getProductData',
                        id_product: parseInt(response.id_product),
                        id_product_attribute: parseInt(response.id_product_attribute),
                        quantity: qty
                    },
                    success:function(data){
                        result = JSON.parse(data);
                        dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
                        dataLayer.push({
                            event: "add_to_cart",
                            currency: result.currency,
                            value: result.product_value,
                            ecommerce: {
                                items: [
                                    {
                                        item_id: result.product_id,
                                        item_name: result.product_name,
                                        affiliation: result.shop_name,
                                        coupon: result.coupon,
                                        discount: result.discount,
                                        index: 0,
                                        item_brand: result.item_brand,
                                        item_category: result.category,
                                        item_list_id: result.item_list_id,
                                        item_list_name: result.item_list_name,
                                        item_variant: result.variant,
                                        price: result.price,
                                        quantity: qty
                                    }
                                ]
                            }
                        });
                    }
                });
            } else if (remove_from_cart_event_name
                && (typeof response.id_product !== 'undefined') && (response.id_product > 0)
                && (typeof response.quantity !== 'undefined') && (response.quantity <= 0)) {
                $.ajax({
                    url: wk_gtm_controller_link,
                    method: 'POST',
                    data: {
                        ajax: 1,
                        wkStaticToken: wkStaticToken,
                        action: 'getProductData',
                        id_product: parseInt(response.id_product),
                        id_product_attribute: parseInt(response.id_product_attribute)
                    },
                    success:function(data){
                        result = JSON.parse(data);
                        dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
                        dataLayer.push({
                            event: "remove_from_cart",
                            ecommerce: {
                                currency: result.currency,
                                value: result.product_value,
                                items: [
                                    {
                                        item_id: result.product_id,
                                        item_name: result.product_name,
                                        affiliation: result.shop_name,
                                        coupon: result.coupon,
                                        currency: result.currency,
                                        discount: result.discount,
                                        index: 0,
                                        item_brand: result.item_brand,
                                        item_category: result.category,
                                        item_list_id: result.item_list_id,
                                        item_list_name: result.item_list_name,
                                        item_variant: result.variant,
                                        price: result.price,
                                        quantity: 0
                                    }
                                ]
                            }
                        });
                    }
                });
            } else if (remove_from_cart_event_name && (typeof response.url !== 'undefined') && response.url.includes('cart?delete=1')) {
                const wk_url = new URLSearchParams(response.url);
                const idProduct = wk_url.get('id_product');
                const idProductAttribute = wk_url.get('id_product_attribute');
                $.ajax({
                    url: wk_gtm_controller_link,
                    method: 'POST',
                    data: {
                        ajax: 1,
                        wkStaticToken: wkStaticToken,
                        action: 'getProductData',
                        id_product: parseInt(idProduct),
                        id_product_attribute: parseInt(idProductAttribute)
                    },
                    success:function(data){
                        result = JSON.parse(data);
                        dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
                        dataLayer.push({
                            event: "remove_from_cart",
                            ecommerce: {
                                currency: result.currency,
                                value: result.product_value,
                                items: [
                                    {
                                        item_id: result.product_id,
                                        item_name: result.product_name,
                                        affiliation: result.shop_name,
                                        coupon: result.coupon,
                                        currency: result.currency,
                                        discount: result.discount,
                                        index: 0,
                                        item_brand: result.item_brand,
                                        item_category: result.category,
                                        item_list_id: result.item_list_id,
                                        item_list_name: result.item_list_name,
                                        item_variant: result.variant,
                                        price: result.price,
                                        quantity: 0
                                    }
                                ]
                            }
                        });
                    }
                });
            }
        });

        prestashop.on('clickQuickView', function(event){
            var idProduct = event.dataset.idProduct;
            var idProductAttribute = event.dataset.id_product_attribute;
            $.ajax({
                url: wk_gtm_controller_link,
                method: 'POST',
                data: {
                    ajax: 1,
                    wkStaticToken: wkStaticToken,
                    action: 'getProductData',
                    id_product: parseInt(idProduct),
                    id_product_attribute: parseInt(idProductAttribute)
                },
                success:function(data) {
                    result = JSON.parse(data);
                    dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
                    dataLayer.push({
                        event: "view_item",
                        ecommerce: {
                            items: [
                                {
                                    item_id: result.product_id,
                                    item_name: result.product_name,
                                    affiliation: result.shop_name,
                                    coupon: result.coupon,
                                    currency: result.currency,
                                    discount: result.discount,
                                    index: 0,
                                    item_brand: result.item_brand,
                                    item_category: result.category,
                                    item_list_id: result.item_list_id,
                                    item_list_name: category_name,
                                    item_variant: result.variant,
                                    price: result.price,
                                    quantity: 1
                                }
                            ]
                        }
                    });
                }
            });
        });
    }
});

function wkGTMShippingInfo(){
    if (controller_name == 'order' && add_shipping_info_event_name) {
        var formElement = $(this);
        if (wk_opc_enabled) {
            var shipping_id = parseInt($('.wk-shipping-carriers [type="radio"]:checked').val());
            var shipping_name = $('.wk-selected-shipping-'+ shipping_id +' .carrier-name').text().trim();
        } else {
            var shipping_id = parseInt($('.delivery-options [type="radio"]:checked').val());
            var shipping_name = $('.delivery-option-'+ shipping_id +' .carriere-name-container').text().trim();
        }
        $.ajax({
            url: wk_gtm_controller_link,
            method: 'POST',
            async: false,
            data: {
                ajax: 1,
                wkStaticToken: wkStaticToken,
                action: 'getAllProductData'
            },
            success:function(data) {
                result = JSON.parse(data);
                products1 = new Array();
                $.each(result['products'],function(i,val) {
                    productdata = {
                        item_id: val.product_id,
                        item_name: val.product_name,
                        affiliation: shop_name,
                        coupon: val.coupon,
                        currency: result.currency,
                        discount: val.discount,
                        index: i,
                        item_brand: val.item_brand,
                        item_category: val.category,
                        item_list_id: val.id_category,
                        item_list_name: val.category,
                        item_variant: val.variant,
                        price: parseFloat(val.price),
                        quantity: val.quantity
                    }
                    products1.push(productdata);
                });
                dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
                dataLayer.push({
                    event: "add_shipping_info",
                    currency: result.currency,
                    value: result.cart_value,
                    coupon: result.coupon,
                    shipping_tier: shipping_name,
                    ecommerce: {
                        items: products1
                    },
                    'eventCallback': function() {
                        formElement.submit();
                    }
                });
            }
        });
    }
}

function wkGTMPaymentInfo(){
    if (controller_name == 'order' && add_payment_info_event_name) {
        var formElement = $(this);
        if (wk_opc_enabled) {
            var payment_div_id = $('[name="payment-option"]:checked').attr('id');
            var wk_payment_type = $('.wk-selected-payment-'+payment_div_id+' p').text().trim();
        } else {
            var payment_div_id = $('[name="payment-option"]:checked').parent().closest('.payment-option').attr('id');
            var wk_payment_type = $('#'+payment_div_id+' label span').text().trim();
        }
        $.ajax({
            url: wk_gtm_controller_link,
            method: 'POST',
            data: {
                ajax: 1,
                wkStaticToken: wkStaticToken,
                action: 'getAllProductData'
            },
            success:function(data) {
                result = JSON.parse(data);
                products1 = new Array();
                $.each(result['products'],function(i,val) {
                    productdata = {
                        item_id: val.product_id,
                        item_name: val.product_name,
                        affiliation: shop_name,
                        coupon: val.coupon,
                        currency: result.currency,
                        discount: val.discount,
                        index: i,
                        item_brand: val.item_brand,
                        item_category: val.category,
                        item_list_id: val.id_category,
                        item_list_name: val.category,
                        item_variant: val.variant,
                        price: parseFloat(val.price),
                        quantity: val.quantity
                    }
                    products1.push(productdata);
                });
                dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
                dataLayer.push({
                    event: "add_payment_info",
                    currency: result.currency,
                    value: result.cart_value,
                    coupon: result.coupon,
                    payment_type: wk_payment_type,
                    ecommerce: {
                        items: products1
                    },
                    'eventCallback': function() {
                        formElement.submit();
                    }
                });
            }
        });
    }
}