PageSourceSearch

https://traffic4you.ru/assets/656d38a/js/store.js

js traffic4you.ru collected 2026-10-04 23:25:57 UTC 12,877 bytes, 390 lines download raw bytes

1$(document).ajaxError(function () {
2    $('#notifications').notify({message: {text: 'Произошла ошибка =('}, 'type': 'danger'}).show();
3});
4
5$(document).ready(function () {
6    var cartWidgetSelector = '#shopping-cart-widget';
7
8    /*страница продукта*/
9    var priceElement = $('#result-price'); //итоговая цена на странице продукта
10    var basePrice = parseFloat($('#base-price').val()); //базовая цена на странице продукта
11    var quantityElement = $('#product-quantity');
12
13    /*корзина*/
14
15    var shippingCostElement = $('#cart-shipping-cost');
16    var cartFullCostElement = $('#cart-full-cost');
17    var cartFullCostWithShippingElement = $('#cart-full-cost-with-shipping');
18
19    function showNotify(element, result, message) {
20        if ($.isFunction($.fn.notify)) {
21            $("#notifications").notify({message: {text: message}, 'type': result}).show();
22        }
23    }
24
25    function updatePrice() {
26        var _basePrice = basePrice;
27        var variants = [];
28        var varElements = $('select[name="ProductVariant[]"]');
29        /* выбираем вариант, меняющий базовую цену максимально*/
30        var hasBasePriceVariant = false;
31        $.each(varElements, function (index, elem) {
32            var varId = elem.value;
33            if (varId) {
34                var option = $(elem).find('option[value="' + varId + '"]');
35                var variant = {amount: option.data('amount'), type: option.data('type')};
36                switch (variant.type) {
37                    case 2: // base price
38                        // еще не было варианта
39                        if (!hasBasePriceVariant) {
40                            _basePrice = variant.amount;
41                            hasBasePriceVariant = true;
42                        }
43                        else {
44                            if (_basePrice < variant.amount) {
45                                _basePrice = variant.amount;
46                            }
47                        }
48                        break;
49                }
50            }
51        });
52        var newPrice = _basePrice;
53        $.each(varElements, function (index, elem) {
54            var varId = elem.value;
55            if (varId) {
56                var option = $(elem).find('option[value="' + varId + '"]');
57                var variant = {amount: option.data('amount'), type: option.data('type')};
58                variants.push(variant);
59                switch (variant.type) {
60                    case 0: // sum
61                        newPrice += variant.amount;
62                        break;
63                    case 1: // percent
64                        newPrice += _basePrice * ( variant.amount / 100);
65                        break;
66                }
67            }
68        });
69
70        priceElement.html(parseFloat(newPrice.toFixed(2)));
71    }
72
73    $('select[name="ProductVariant[]"]').change(function () {
74        updatePrice();
75    });
76
77
78    $('.product-quantity-increase').click(function () {
79        quantityElement.val(parseInt(quantityElement.val()) + 1);
80    });
81
82    $('.product-quantity-decrease').click(function () {
83        if (parseInt(quantityElement.val()) > 1) {
84            quantityElement.val(parseInt(quantityElement.val()) - 1);
85        }
86    });
87
88    function updateCartWidget() {
89        $(cartWidgetSelector).load($('#cart-widget').data('cart-widget-url'));
90    }
91
92    $('#add-product-to-cart').click(function (e) {
93        e.preventDefault();
94        var button = $(this);
95        button.button('loading');
96        var form = $(this).parents('form');
97        $.ajax({
98            type: 'post',
99            dataType: 'json',
100            data: form.serialize(),
101            url: form.attr('action'),
102            success: function (data) {
103                if (data.result) {
104                    updateCartWidget();
105                }
106                showNotify(button, data.result ? 'success' : 'danger', data.data);
107            }
108        }).always(function () {
109            button.button('reset');
110        });
111    });
112
113    $('body').on('click', '.quick-add-product-to-cart', function (e) {
114        e.preventDefault();
115        var el = $(this);
116        var data = {'Product[id]': el.data('product-id')};
117        data[yupeTokenName] = yupeToken;
118        $.ajax({
119            url: el.data('cart-add-url'),
120            type: 'post',
121            data: data,
122            dataType: 'json',
123            success: function (data) {
124                if (data.result) {
125                    updateCartWidget();
126                }
127                showNotify(el, data.result ? 'success' : 'danger', data.data);
128            }
129        });
130    });
131
132
133    /*cart*/
134
135    function getCoupons() {
136        var coupons = [];
137        $.each($('.coupon-input'), function (index, elem) {
138            var $elem = $(elem);
139            coupons.push({
140                code: $elem.data('code'),
141                name: $elem.data('name'),
142                value: $elem.data('value'),
143                type: $elem.data('type'),
144                min_order_price: $elem.data('min-order-price'),
145                free_shipping: $elem.data('free-shipping')
146            })
147        });
148        return coupons;
149    }
150
151    function updatePositionSumPrice(tr) {
152        var count = parseInt(tr.find('.position-count').val());
153        var price = parseFloat(tr.find('.position-price').text());
154        tr.find('.position-sum-price').html(price * count);
155        updateCartTotalCost();
156    }
157
158    function changePositionQuantity(productId, quantity) {
159        var data = {'quantity': quantity, 'id': productId};
160        data[yupeTokenName] = yupeToken;
161        $.ajax({
162            url: yupeCartUpdateUrl,
163            type: 'post',
164            data: data,
165            dataType: 'json',
166            success: function (data) {
167                if (data.result) {
168                    updateCartWidget();
169                }
170            }
171        });
172    }
173
174    $('.cart-quantity-increase').click(function () {
175        var target = $($(this).data('target'));
176        target.val(parseInt(target.val()) + 1).trigger('change');
177    });
178
179    $('.cart-quantity-decrease').click(function () {
180        var target = $($(this).data('target'));
181        if (parseInt(target.val()) > 1) {
182            target.val(parseInt(target.val()) - 1).trigger('change');
183        }
184    });
185
186    $('.cart-delete-product').click(function (e) {
187        e.preventDefault();
188        var el = $(this);
189        var data = {'id': el.data('position-id')};
190        data[yupeTokenName] = yupeToken;
191        $.ajax({
192            url: yupeCartDeleteProductUrl,
193            type: 'post',
194            data: data,
195            dataType: 'json',
196            success: function (data) {
197                if (data.result) {
198                    el.parents('tr').remove();
199                    updateCartTotalCost();
200                    if ($('.cart-position').length > 0) {
201                        updateCartWidget();
202                    } else {
203                        $('#cart-body').html(yupeCartEmptyMessage);
204                    }
205                }
206            }
207        });
208    });
209
210    $('.position-count').change(function () {
211        var tr = $(this).parents('tr');
212        updatePositionSumPrice(tr);
213        var quantity = tr.find('.position-count').val();
214        var productId = tr.find('.position-id').val();
215        changePositionQuantity(productId, quantity);
216    });
217
218    $('input[name="Order[delivery_id]"]').change(function () {
219        updateShippingCost();
220    });
221
222    function getCartTotalCost() {
223        var cost = 0;
224        $.each($('.position-sum-price'), function (index, elem) {
225            cost += parseFloat($(elem).text());
226        });
227        var delta = 0;
228        var coupons = getCoupons();
229        $.each(coupons, function (index, el) {
230            if (cost >= el.min_order_price) {
231                switch (el.type) {
232                    case 0: // руб
233                        delta += parseFloat(el.value);
234                        break;
235                    case 1: // %
236                        delta += (parseFloat(el.value) / 100) * cost;
237                        break;
238                }
239            }
240        });
241
242        return delta > cost ? 0 : cost - delta;
243    }
244
245    function updateCartTotalCost() {
246        cartFullCostElement.html(getCartTotalCost());
247        refreshDeliveryTypes();
248        updateShippingCost();
249        updateFullCostWithShipping();
250    }
251
252    function refreshDeliveryTypes() {
253        var cartTotalCost = getCartTotalCost();
254        $.each($('input[name="Order[delivery_id]"]'), function (index, el) {
255            var elem = $(el);
256            var availableFrom = elem.data('available-from');
257            if (availableFrom.length && parseFloat(availableFrom) >= cartTotalCost) {
258                if (elem.prop('checked')) {
259                    checkFirstAvailableDeliveryType();
260                }
261                elem.prop('disabled', true);
262            } else {
263                elem.prop('disabled', false);
264            }
265        });
266    }
267
268    function checkFirstAvailableDeliveryType() {
269        $('input[name="Order[delivery_id]"]:not(:disabled):first').prop('checked', true);
270    }
271
272
273    function getShippingCost() {
274        var cartTotalCost = getCartTotalCost();
275        var coupons = getCoupons();
276        var freeShipping = false;
277        $.each(coupons, function (index, el) {
278            if (el.free_shipping && cartTotalCost >= el.min_order_price) {
279                freeShipping = true;
280            }
281        });
282        if (freeShipping) {
283            return 0;
284        }
285        var selectedDeliveryType = $('input[name="Order[delivery_id]"]:checked');
286        if (!selectedDeliveryType[0]) {
287            return 0;
288        }
289        if (parseInt(selectedDeliveryType.data('separate-payment')) || parseFloat(selectedDeliveryType.data('free-from')) <= cartTotalCost) {
290            return 0;
291        } else {
292            return parseFloat(selectedDeliveryType.data('price'));
293        }
294    }
295
296    function updateShippingCost() {
297        shippingCostElement.html(getShippingCost());
298        updateFullCostWithShipping();
299    }
300
301    function updateFullCostWithShipping() {
302        cartFullCostWithShippingElement.html(getShippingCost() + getCartTotalCost());
303    }
304
305    refreshDeliveryTypes();
306    //checkFirstAvailableDeliveryType();
307    //updateFullCostWithShipping();
308    //updateCartTotalCost();
309
310    function updateAllCosts() {
311        updateCartTotalCost();
312    }
313
314    checkFirstAvailableDeliveryType();
315    updateAllCosts();
316
317    $('#start-payment').on('click', function () {
318        $('.payment-method-radio:checked').parents('.payment-method').find('form').submit();
319    });
320
321    $('body').on('click', '.clear-cart', function (e) {
322        e.preventDefault();
323        var data = {};
324        data[yupeTokenName] = yupeToken;
325        $.ajax({
326            url: '/coupon/clear',
327            type: 'post',
328            data: data,
329            dataType: 'json',
330            success: function (data) {
331                if (data.result) {
332                    updateCartWidget();
333                }
334            }
335        });
336    });
337
338    $('#add-coupon-code').click(function (e) {
339        e.preventDefault();
340        var code = $('#coupon-code').val();
341        var button = $(this);
342        if (code) {
343            var data = {'code': code};
344            data[yupeTokenName] = yupeToken;
345            $.ajax({
346                url: '/coupon/add',
347                type: 'post',
348                data: data,
349                dataType: 'json',
350                success: function (data) {
351                    if (data.result) {
352                        window.location.reload();
353                    }
354                    showNotify(button, data.result ? 'success' : 'danger', data.data.join('; '));
355                }
356            });
357            $('#coupon-code').val('');
358        }
359    });
360
361    $('.coupon .close').click(function (e) {
362        e.preventDefault();
363        var code = $(this).siblings('input[type="hidden"]').data('code');
364        var data = {'code': code};
365        data[yupeTokenName] = yupeToken;
366        $.ajax({
367            url: '/coupon/remove',
368            type: 'post',
369            data: data,
370            dataType: 'json',
371            success: function (data) {
372                showNotify(this, data.result ? 'success' : 'danger', data.data);
373                if (data.result) {
374                    updateAllCosts();
375                }
376            }
377        });
378    });
379
380    $('#coupon-code').keypress(function (e) {
381        if (e.which == 13) {
382            e.preventDefault();
383            $('#add-coupon-code').click();
384        }
385    });
386
387    $('.order-form').submit(function () {
388        $(this).find("button[type='submit']").prop('disabled', true);
389    });
390});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.