From 29cb32aa2e4f9a541d7b000c689406ab0f01d5ff Mon Sep 17 00:00:00 2001 From: maksim Date: Wed, 18 Jun 2025 19:04:30 +0300 Subject: [PATCH] =?UTF-8?q?Task=206807=20|=20=D1=84=D0=B8=D0=BA=D1=81=20?= =?UTF-8?q?=D0=B1=D0=B0=D0=B3=D0=BE=D0=B2=20=D0=BF=D1=80=D0=B8=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=B2?= =?UTF-8?q?=20=D0=BA=D0=BE=D1=80=D0=B7=D0=B8=D0=BD=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shop/components/cart/assets/js/cart.js | 26 +---------------- .../temp-functions/woocommerce-logic.php | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 25 deletions(-) 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(); // выводим уведомления, если они есть