|
|
|
@ -6,18 +6,40 @@ jQuery(document).ready(function($) { |
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// Обновление количества через input
|
|
|
|
|
$(document).on('change', '.counter__input', function() { |
|
|
|
|
const key = $(this).data('key'); |
|
|
|
|
const quantity = $(this).val(); |
|
|
|
|
$(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); |
|
|
|
|
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 }) + ' <span class="woocommerce-Price-currencySymbol">₽</span>' |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
// Обновить значение по умолчанию (чтобы расчёт 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) { |
|
|
|
|
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); |
|
|
|
|