Task 6807 | фикс багов при добавлении в корзину

pull/36/head
parent 37e3008e51
commit 29cb32aa2e
  1. 26
      wp-content/themes/cosmopet/modules/shop/components/cart/assets/js/cart.js
  2. 29
      wp-content/themes/cosmopet/temp-functions/woocommerce-logic.php

@ -12,31 +12,7 @@ jQuery(document).ready(function($) {
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 }) +
'&nbsp;<span class="woocommerce-Price-currencySymbol">' + currencySymbol + '</span>'
);
// Обновить значение по умолчанию (чтобы расчёт unitPrice был корректен в следующий раз)
$input.prop('defaultValue', quantity);
}
updateCart(key, quantity);
} else {
removeItem(key);
}

@ -341,6 +341,35 @@ add_filter('woocommerce_cart_redirect_after_error', '__return_false');
add_action('wp_ajax_get_wc_notices', 'custom_get_wc_notices');
add_action('wp_ajax_nopriv_get_wc_notices', 'custom_get_wc_notices');
add_action('woocommerce_after_cart_item_quantity_update', 'custom_limit_cart_quantity_on_update', 20, 4);
function custom_limit_cart_quantity_on_update($cart_item_key, $quantity, $old_quantity, $cart) {
$cart_item = $cart->get_cart_item($cart_item_key);
if (!$cart_item) {
return;
}
$product = $cart_item['data'];
if (!$product->managing_stock()) {
return;
}
$stock = $product->get_stock_quantity();
if ($quantity > $stock) {
// Ограничим до доступного количества
$cart->set_quantity($cart_item_key, $stock);
wc_add_notice(sprintf(
__('Вы запросили %d шт., но доступно только %d. Количество было скорректировано.', 'woocommerce'),
$quantity, $stock
), 'notice');
}
}
function custom_get_wc_notices() {
ob_start();
wc_print_notices(); // выводим уведомления, если они есть

Loading…
Cancel
Save