Untitled

mail@pastecode.io avatar
unknown
plain_text
8 days ago
3.5 kB
2
Indexable
Never
isOnAfterPaymentPage: function () {
        function decodeHtml(html) {
            return Insider.dom('<div>').html(html).text();
        }

        function removeDuplicates(array) {
            var uniqueElements = {};
            var uniqueArray = [];

            for (var i = 0; i < array.length; i++) {
                var key = array[i];

                if (!uniqueElements[key]) {
                    uniqueElements[key] = true;
                    uniqueArray.push(array[i]);
                }
            }

            return uniqueArray;
        }

        var pageConfirmation = Insider.utils.getDataFromIO('page', 'type').toLowerCase() === 'confirmation';

        if (pageConfirmation && !Insider.__external.afterPaymentStoragesUpdated) {
            Insider.__external.afterPaymentStoragesUpdated = true;

            var line_items = Insider.utils.getDataFromIO('transaction', 'line_items', []);

            if (line_items.length > 0) {
                var productList = [];
                var totalCartAmount = 0;
                var totalQuantity = 0;
                var currency = Insider.systemRules.call('getCurrency');
                var preferredCurrency = Insider.currencyService.to;

                line_items.forEach(function (item) {
                    var price = item.product.unit_sale_price || item.product.unit_price || 0;

                    if (price > 0) {
                        productList.push({
                            cats: removeDuplicates(item.product.taxonomy).slice(0, 50),
                            exchange: 'from ' + currency + ' to ' + preferredCurrency,
                            id: item.product.id || '',
                            img: item.product.product_image_url || '',
                            name: encodeURIComponent(decodeHtml(item.product.name || '')),
                            originalPrice: item.product.unit_price || 0,
                            notConvertedPrice: price,
                            price: price,
                            quantity: item.quantity || 1,
                            time: Insider.dateHelper.now(),
                            url: item.product.url || '',
                        });
                        totalQuantity += item.quantity;
                        totalCartAmount += item.subtotal;
                    }
                });

                productList = Insider.currencyService.getConvertedPrice(currency, preferredCurrency, productList);
                totalCartAmount = Insider.currencyService.getConvertedPrice(currency, preferredCurrency,
                    totalCartAmount);
                Insider.utils.cart.storeCartProductStorage({
                    totalQuantity: totalQuantity,
                    productList: productList
                });

                Insider.storage.localStorage.set({
                    name: 'paid-products',
                    value: productList
                });

                Insider.storage.localStorage.set({
                    name: 'total-cart-amount',
                    value: parseFloat(totalCartAmount.toFixed(2)) || 0
                });

                setTimeout(function () {
                    Insider.eventManager.dispatch('cart:amount:update');
                }, 250);
            }
        }

        return pageConfirmation || !!Insider.__external.afterPaymentStoragesUpdated;
    }
Leave a Comment