|
|
|
@ -6,42 +6,41 @@ jQuery(document).ready(function($) { |
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// Обновление количества через input
|
|
|
|
|
$(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; |
|
|
|
|
|
|
|
|
|
// Получаем текущий символ валюты из разметки
|
|
|
|
|
const currencySymbol = $priceElement.find('.woocommerce-Price-currencySymbol').text().trim(); |
|
|
|
|
|
|
|
|
|
// Формируем новую цену с тем же символом
|
|
|
|
|
$priceElement.find('bdi').html( |
|
|
|
|
newTotal.toLocaleString('ru-RU', { minimumFractionDigits: 0 }) + |
|
|
|
|
' <span class="woocommerce-Price-currencySymbol">' + currencySymbol + '</span>' |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
// Обновить значение по умолчанию (чтобы расчёт unitPrice был корректен в следующий раз)
|
|
|
|
|
$input.prop('defaultValue', 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; |
|
|
|
|
|
|
|
|
|
// Получаем текущий символ валюты из разметки
|
|
|
|
|
const currencySymbol = $priceElement.find('.woocommerce-Price-currencySymbol').text().trim(); |
|
|
|
|
|
|
|
|
|
// Формируем новую цену с тем же символом
|
|
|
|
|
$priceElement.find('bdi').html( |
|
|
|
|
newTotal.toLocaleString('ru-RU', { minimumFractionDigits: 0 }) + |
|
|
|
|
' <span class="woocommerce-Price-currencySymbol">' + currencySymbol + '</span>' |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
// Обновить значение по умолчанию (чтобы расчёт unitPrice был корректен в следующий раз)
|
|
|
|
|
$input.prop('defaultValue', quantity); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
removeItem(key); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$(document).on('click', '.modal__basket .counter__button.plus', function(e) { |
|
|
|
@ -406,4 +405,69 @@ $(document).on('change', '.counter__input', function () { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
$('.detail form.cart').on('submit', function(e) { |
|
|
|
|
e.preventDefault(); |
|
|
|
|
|
|
|
|
|
var $form = $(this); |
|
|
|
|
product_id = $form.find('button[name=add-to-cart]').val(), |
|
|
|
|
quantity = $form.find('input.qty').val() || 1; |
|
|
|
|
console.log(product_id); |
|
|
|
|
$.ajax({ |
|
|
|
|
url: wc_add_to_cart_params.ajax_url, |
|
|
|
|
type: 'POST', |
|
|
|
|
data: { |
|
|
|
|
action: 'woocommerce_ajax_add_to_cart', |
|
|
|
|
product_id: product_id, |
|
|
|
|
quantity: quantity, |
|
|
|
|
}, |
|
|
|
|
success: function(response) { |
|
|
|
|
if (response.error && response.product_url) { |
|
|
|
|
window.location = response.product_url; |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
updateCartFragment(); |
|
|
|
|
openBasketOnFirstAdd(); |
|
|
|
|
}, |
|
|
|
|
error: function(xhr) { |
|
|
|
|
console.log('AJAX Error:', xhr.responseText); |
|
|
|
|
alert('Ошибка добавления товара.'); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
jQuery(function($){ |
|
|
|
|
$('form.cart').on('submit', function(e){ |
|
|
|
|
e.preventDefault(); |
|
|
|
|
|
|
|
|
|
var $form = $(this); |
|
|
|
|
var $button = $form.find('button[type="submit"]'); |
|
|
|
|
|
|
|
|
|
var product_id = $button.val(); |
|
|
|
|
var quantity = $form.find('input.qty').val() || 1; |
|
|
|
|
$.ajax({ |
|
|
|
|
type: 'POST', |
|
|
|
|
url: wc_add_to_cart_params.ajax_url, |
|
|
|
|
data: { |
|
|
|
|
action: 'woocommerce_ajax_add_to_cart', |
|
|
|
|
product_id: product_id, |
|
|
|
|
quantity: quantity, |
|
|
|
|
}, |
|
|
|
|
beforeSend: function() { |
|
|
|
|
$button.addClass('loading'); |
|
|
|
|
}, |
|
|
|
|
success: function(response) { |
|
|
|
|
if (response.error && response.product_url) { |
|
|
|
|
window.location = response.product_url; |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$(document.body).trigger('added_to_cart', [response.fragments, response.cart_hash, $button]); |
|
|
|
|
$button.removeClass('loading'); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
}); |