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 6dd6a14..9b91cb3 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 @@ -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 }) + - ' ' + currencySymbol + '' - ); - - // Обновить значение по умолчанию (чтобы расчёт unitPrice был корректен в следующий раз) - $input.prop('defaultValue', quantity); - } + updateCart(key, quantity); } else { removeItem(key); } diff --git a/wp-content/themes/cosmopet/temp-functions/woocommerce-logic.php b/wp-content/themes/cosmopet/temp-functions/woocommerce-logic.php index ef63e8f..5d991d1 100644 --- a/wp-content/themes/cosmopet/temp-functions/woocommerce-logic.php +++ b/wp-content/themes/cosmopet/temp-functions/woocommerce-logic.php @@ -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(); // выводим уведомления, если они есть