From 3da657dbcb8b79cb74e46d8641e493ad136ee707 Mon Sep 17 00:00:00 2001 From: "vishalsingh.qlo325" Date: Fri, 20 Jun 2025 18:51:06 +0530 Subject: [PATCH] Fixed: added stock wise restrictions on standalone service products --- controllers/front/CartController.php | 4 ++-- controllers/front/ProductController.php | 9 +++++++ modules/blockcart/ajax-cart.js | 4 ++++ .../_partials/booking-form.tpl | 24 +++++++++++++++---- .../js/modules/blockcart/ajax-cart.js | 4 ++++ themes/hotel-reservation-theme/js/product.js | 22 ++++++++++++----- themes/hotel-reservation-theme/product.tpl | 1 + 7 files changed, 56 insertions(+), 12 deletions(-) diff --git a/controllers/front/CartController.php b/controllers/front/CartController.php index b11c0e54a..2859869ae 100644 --- a/controllers/front/CartController.php +++ b/controllers/front/CartController.php @@ -474,7 +474,7 @@ class CartControllerCore extends FrontController $finalQuantity += $quantityInCart; } if ($product->max_quantity && $finalQuantity > $product->max_quantity) { - $this->errors[] = Tools::displayError(sprintf('You can now add more than %d quantity for this product in the cart.', $product->max_quantity)); + $this->errors[] = Tools::displayError(sprintf('You cannot add more than %d quantity for this product in the cart.', $product->max_quantity)); } } else { if ($id_cart) { @@ -517,7 +517,7 @@ class CartControllerCore extends FrontController $finalQuantity += $productCartDetail['quantity']; } if ($product->max_quantity && $finalQuantity > $product->max_quantity) { - $this->errors[] = Tools::displayError(sprintf('You can not add more than %d quantity for this product in the cart.', $product->max_quantity)); + $this->errors[] = Tools::displayError(sprintf('You cannot add more than %d quantity for this product in the cart.', $product->max_quantity)); } } elseif ($productCartDetail) { $this->errors[] = Tools::displayError('You can only order one quantity for this product.'); diff --git a/controllers/front/ProductController.php b/controllers/front/ProductController.php index 24cc682af..d265cba75 100644 --- a/controllers/front/ProductController.php +++ b/controllers/front/ProductController.php @@ -496,6 +496,15 @@ class ProductControllerCore extends FrontController $this->context->smarty->assign('error', Tools::getValue('error')); } } else { + if ($this->product->allow_multiple_quantity) { + if ($products = $this->context->cart->getProducts()) { + $products = array_column($products, 'cart_quantity', 'id_product'); + if (isset($products[$this->product->id])) { + $this->product->quantity -= $products[$this->product->id]; + $this->product->max_quantity -= $products[$this->product->id]; + } + } + } $this->assignServiceProductVars(); } diff --git a/modules/blockcart/ajax-cart.js b/modules/blockcart/ajax-cart.js index 47836bee8..0c25d3a45 100644 --- a/modules/blockcart/ajax-cart.js +++ b/modules/blockcart/ajax-cart.js @@ -432,6 +432,10 @@ var ajaxCart = { resetRoomtypeServices(); + if (parseInt($('#product_page_booking_product').val()) == 0) { + $('#service_product_qty').val(1); + } + BookingForm.refresh(true); } diff --git a/themes/hotel-reservation-theme/_partials/booking-form.tpl b/themes/hotel-reservation-theme/_partials/booking-form.tpl index 1a592f740..adc6d7f2c 100644 --- a/themes/hotel-reservation-theme/_partials/booking-form.tpl +++ b/themes/hotel-reservation-theme/_partials/booking-form.tpl @@ -245,16 +245,25 @@ {/if} {/block} {block name='booking_form_service_product_quantity'} + {assign var='is_out_of_stock' value=false} + {assign var='max_qty_reached' value=false} + {if (!$allow_oosp && $product->quantity <= 0) || !$product->available_for_order || (isset($restricted_country_mode) && $restricted_country_mode) || $PS_CATALOG_MODE} + {assign var='is_out_of_stock' value=true} + {else if $product->allow_multiple_quantity && !$product->max_quantity} + {assign var='max_qty_reached' value=true} + {/if} + {if $product->allow_multiple_quantity}
-
quantity <= 0) || !$product->available_for_order || $PS_CATALOG_MODE} style="display: none;"{/if}> + - {if $product->available_for_order} + {if !$is_out_of_stock || !$max_qty_reached}
{/if} {else} @@ -301,7 +310,14 @@ {/block}
- {if (!$allow_oosp && $product->quantity <= 0) || !$product->available_for_order || (isset($restricted_country_mode) && $restricted_country_mode) || $PS_CATALOG_MODE} + {if $is_out_of_stock} +
+ {l s='Product is out of stock!'} +
+ {else if $max_qty_reached} +
+ {l s='Max quantity reached!'} +
{else} {block name='booking_form_book_now_button'}

diff --git a/themes/hotel-reservation-theme/js/modules/blockcart/ajax-cart.js b/themes/hotel-reservation-theme/js/modules/blockcart/ajax-cart.js index 47836bee8..0c25d3a45 100644 --- a/themes/hotel-reservation-theme/js/modules/blockcart/ajax-cart.js +++ b/themes/hotel-reservation-theme/js/modules/blockcart/ajax-cart.js @@ -432,6 +432,10 @@ var ajaxCart = { resetRoomtypeServices(); + if (parseInt($('#product_page_booking_product').val()) == 0) { + $('#service_product_qty').val(1); + } + BookingForm.refresh(true); } diff --git a/themes/hotel-reservation-theme/js/product.js b/themes/hotel-reservation-theme/js/product.js index 22e483de5..8e54f23d4 100644 --- a/themes/hotel-reservation-theme/js/product.js +++ b/themes/hotel-reservation-theme/js/product.js @@ -1206,13 +1206,23 @@ $(document).ready(function() { e.preventDefault(); qtyfield = $(this).closest('.qty_container').find('input.service_product_qty'); var newQuantity = parseInt(qtyfield.val()) + 1; - if (qtyfield.data('max_quantity') && qtyfield.data('max_quantity') < newQuantity) { - // if max quantity is reached - newQuantity = qtyfield.data('max_quantity'); - - showErrorMessage(max_service_product_qty_txt+' '+qtyfield.data('max_quantity')); + let hasStockError = false; + if (parseInt($('#product_page_booking_product').val()) == 0) { + let stockQty = parseInt($(this).closest('.qty_container').find('.stock_qty').data('stock_quantity')); + if (stockQty < newQuantity) { + showErrorMessage(out_of_stock_text); + newQuantity = stockQty; + } + } + if (!hasStockError) { + if (qtyfield.data('max_quantity') && qtyfield.data('max_quantity') < newQuantity) { + // if max quantity is reached + newQuantity = qtyfield.data('max_quantity'); + + showErrorMessage(max_service_product_qty_txt+' '+qtyfield.data('max_quantity')); + } + $(this).closest('.qty_container').find('.qty_count span').text(newQuantity); } - $(this).closest('.qty_container').find('.qty_count span').text(newQuantity); qtyfield.val(newQuantity); clearTimeout(timer); timer = setTimeout(function() { diff --git a/themes/hotel-reservation-theme/product.tpl b/themes/hotel-reservation-theme/product.tpl index 351edf8f8..9492fbc86 100644 --- a/themes/hotel-reservation-theme/product.tpl +++ b/themes/hotel-reservation-theme/product.tpl @@ -877,6 +877,7 @@ {addJsDefL name=service_removed_txt}{l s='Service removed' js=1}{/addJsDefL} {addJsDefL name=service_updated_txt}{l s='Service updated' js=1}{/addJsDefL} {addJsDefL name=max_service_product_qty_txt}{l s='Maximum allowed quantity in the cart is' js=1}{/addJsDefL} + {addJsDefL name=out_of_stock_text}{l s='Requested quantitity is out of stock' js=1}{/addJsDefL} {/strip} {/block} {/if}