From d1e33b17d2deba0acbf08c73c2a6f189ef2d5225 Mon Sep 17 00:00:00 2001 From: maksim Date: Wed, 11 Jun 2025 10:23:21 +0300 Subject: [PATCH] =?UTF-8?q?Fix=20|=20=D0=A4=D0=B8=D0=BA=D1=81=20=D0=B1?= =?UTF-8?q?=D0=B0=D0=B3=D0=B0=20=D0=BF=D1=80=D0=B8=20=D0=BE=D0=B4=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=BD=D0=BE=D0=BC=20?= =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BB-=D0=B2=D0=B0=20=D1=82=D0=BE=D0=B2=D0=B0=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=B8=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B8=20=D1=82=D0=BE=D0=B2=D0=B0=D1=80=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=B8=D0=B7=20=D0=BC=D0=B8=D0=BD=D0=B8-=D0=BA=D0=BE=D1=80?= =?UTF-8?q?=D0=B7=D0=B8=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/footer/assets/js/footer.js | 123 ------------------ .../shop/components/cart/assets/js/cart.js | 57 +++++--- 2 files changed, 41 insertions(+), 139 deletions(-) diff --git a/wp-content/themes/cosmopet/modules/footer/assets/js/footer.js b/wp-content/themes/cosmopet/modules/footer/assets/js/footer.js index 1947f31..a784a0a 100644 --- a/wp-content/themes/cosmopet/modules/footer/assets/js/footer.js +++ b/wp-content/themes/cosmopet/modules/footer/assets/js/footer.js @@ -135,97 +135,6 @@ }, }); }); - - $('body').on('click', '.modal-basket__item .counter__button', function(){ - var input = $(this).closest('.counter').find('.counter__input').val() - var price = $(this).closest('.modal-basket-item__control').find('.modal-basket-item__price') - var productID = $(this).data('id') - var pr = $(this).closest('.modal-basket__item').find('.modal-basket__item-before').data('pr') - $(price).html(Number(input) * $(price).data('basecost')) - $(this).closest('.modal-basket__item').data('cost', Number(price.html())) - $('#cart_total').html(wc_cart_summary_upd()) - var title = $(this).closest('.cart_item').find('.modal-basket-item__title').html() - if ($(this).hasClass('minus')){ - $(this).attr('data-value', Number( $(this).attr('data-value')) - 1) - - if(Number($(this).attr('data-value')) == 0){ - $(this).closest('.modal-basket__item').find('.modal-basket__item-before').click() - } - dataLayer.push({ - "ecommerce": { - "currencyCode": "RUB", - "remove": { - "products": [ - { - "id": pr, - "name": title, - "quantity": 1, - } - ] - } - } - }); - } - else{ - var min = $(this).closest('.counter--small').find('.minus') - $(min).attr('data-value', Number($(min).attr('data-value')) + 1) - dataLayer.push({ - "ecommerce": { - "currencyCode": "RUB", - "add": { - "products": [ - { - "id": pr, - "name": title, - "quantity": 1, - } - ] - } - } - }); - } - // if ($(this).hasClass('minus') && Number(input) == 1){ - // $(this).closest('.modal-basket__item').find('.modal-basket__item-before').click() - // } - var data = { - action: 'get_cartprice', - quantity: input, - product: productID - }; - $.ajax({ - type: 'post', - url: '/wp-admin/admin-ajax.php', - data: data, - beforeSend: function (response) { - // $thisbutton.removeClass('added').addClass('loading'); - }, - complete: function (response) { - // $thisbutton.addClass('added').removeClass('loading'); - }, - success: function (response) { - if (response.error) { - - } else { -// $('#cart_total').html(response) - initCounters() - if(response['cart_count'] > 0){ - $('.mini-profile__button--counter').removeClass('disabled').html(response['cart_count']) - } - else{ - $('.mini-profile__button--counter').addClass('disabled').html(response['cart_count']) - } - } - }, - }); - }) - - // $('body').on('click', '.product-item-overlay__field .counter__button', function(){ - // var input = $(this).closest('.counter').find('.counter__input').val() - // var price = $(this).closest('.product-item__form').find('.product-item-overlay__price') - - - // $(price).html(Number(input) * $(price).data('basecost')) - // }) $('body').on('click', '.modal-basket__item-before', function(){ var key = $(this).data('id') @@ -723,38 +632,6 @@ selects.forEach(select => { } initSelect() -// counter -function initCounters(){ - let counters = document.querySelectorAll('.counter'); - counters.forEach(e => { - let minus = e.querySelector('.minus'), - plus = e.querySelector('.plus'), - input = e.querySelector('.counter__input'); - minus.onclick = function (e) { - e.preventDefault(); - - let number = input.value; - - if (number >= 2){ - input.value = Number(number) - 1; - input.setAttribute('value', input.value) - } - } - - plus.onclick = function (e) { - e.preventDefault(); - - let number = input.value; - let max = input.dataset.maxcount - if (number <= max - 1) { - input.value = Number(number) + 1; - input.setAttribute('value', input.value) - } - } - }) -} - -initCounters() // counter // checkbox diff --git a/wp-content/themes/cosmopet/modules/shop/components/cart/assets/js/cart.js b/wp-content/themes/cosmopet/modules/shop/components/cart/assets/js/cart.js index 53162d7..5a401c2 100644 --- a/wp-content/themes/cosmopet/modules/shop/components/cart/assets/js/cart.js +++ b/wp-content/themes/cosmopet/modules/shop/components/cart/assets/js/cart.js @@ -6,18 +6,40 @@ jQuery(document).ready(function($) { // }); // Обновление количества через input - $(document).on('change', '.counter__input', function() { - const key = $(this).data('key'); - const quantity = $(this).val(); - - if (quantity > 0) { - updateCart(key, quantity); - } else { - removeItem(key); +$(document).on('change', '.counter__input', function () { + const $input = $(this); + const key = $input.data('key'); + const quantity = parseInt($input.val(), 10); + + if (quantity > 0) { + updateCart(key, quantity, false); + + const $item = $input.closest('.modal-basket-item__control'); + const $priceElement = $item.find('.woocommerce-Price-amount'); + const currentPriceText = $priceElement.text().replace(/[^\d,]/g, '').replace(',', '.'); + const currentTotal = parseFloat(currentPriceText); + + // Защита от деления на 0 + if (!isNaN(currentTotal) && currentTotal > 0) { + const oldQuantity = parseInt($input.prop('defaultValue'), 10); + const unitPrice = currentTotal / oldQuantity; + const newTotal = unitPrice * quantity; + + // Обновить цену + $priceElement.find('bdi').html( + newTotal.toLocaleString('ru-RU', { minimumFractionDigits: 0 }) + ' ' + ); + + // Обновить значение по умолчанию (чтобы расчёт unitPrice был корректен в следующий раз) + $input.prop('defaultValue', quantity); } - }); + } else { + removeItem(key); + } +}); + - // Увеличение количества + $(document).on('click', '.modal__basket .counter__button.plus', function(e) { e.preventDefault(); const key = $(this).data('key'); @@ -26,7 +48,7 @@ jQuery(document).ready(function($) { input.val(quantity).trigger('change'); }); - // Уменьшение количества + // // Уменьшение количества $(document).on('click', '.modal__basket .counter__button.minus', function(e) { e.preventDefault(); const key = $(this).data('key'); @@ -192,7 +214,8 @@ jQuery(document).ready(function($) { ); localStorage.setItem(removedItemsStorageKey, JSON.stringify(updatedItems)); - updateCartFragment(); + updateCartFragment(false); + $('[data-key="' + key + '"]').remove() } } }); @@ -294,7 +317,7 @@ jQuery(document).ready(function($) { }); // Функция обновления количества - function updateCart(key, quantity) { + function updateCart(key, quantity, update_full = true) { $.ajax({ type: 'POST', url: '/wp-admin/admin-ajax.php', // Используем стандартный параметр WooCommerce @@ -311,7 +334,7 @@ jQuery(document).ready(function($) { }, success: function(response) { if (response.success) { - updateCartFragment(); + updateCartFragment(update_full); } else { console.error('Ошибка при обновлении корзины'); } @@ -323,7 +346,7 @@ jQuery(document).ready(function($) { } // Обновление фрагментов корзины - function updateCartFragment() { + function updateCartFragment(update_full=true) { $.ajax({ type: 'POST', url: woocommerce_params.ajax_url, @@ -339,7 +362,9 @@ jQuery(document).ready(function($) { success: function(response) { console.log(response); if (response.success) { - $('#modal-basket-content').html(response.data.contents); + if (update_full){ + $('#modal-basket-content').html(response.data.contents); + } $('.modal-block-price__price').html(response.data.total); $('#modal-basket-footer').html(response.data.cart_bottom) $('.mini-profile__button--counter').text(response.data.count);