@@ -278,9 +278,36 @@ const sendUpdateCartRequest = async (updateUrl: string, quantity: number) => {
278278 return response ;
279279} ;
280280
281+ const populateMinQuantityInput = ( selector = quantityInputMap . default ) => {
282+ const qtyInputNodeList = document . querySelectorAll < HTMLElement > ( selector ) ;
283+
284+ // For each product in list
285+ qtyInputNodeList . forEach ( ( qtyInputWrapper : HTMLElement ) => {
286+ const idProduct = qtyInputWrapper . closest ( 'form' )
287+ ?. querySelector < HTMLInputElement > ( quantityInputMap . idProductInput ) ?. value ;
288+ const qtyInput = qtyInputWrapper . querySelector < HTMLInputElement > ( 'input' ) ;
289+
290+ // if the idproduct is set, and the input has a min attribute
291+ if ( idProduct && qtyInput && qtyInput . dataset . min ) {
292+ // we check if the product is already in the cart
293+ const productInCart = window . prestashop . cart . products . filter (
294+ ( product : { id : number } ) => product . id === parseInt ( idProduct , 10 ) ,
295+ ) . shift ( ) ;
296+ // if the product is in the cart (and if the qty wanted is >= than the min qty, we set the minimal quantity to 1
297+ const minimalQuantity = productInCart && productInCart . quantity_wanted >= qtyInput . dataset . min
298+ ? 1 : qtyInput . dataset . min ;
299+ // we set the min attribute to the input
300+ qtyInput . setAttribute ( 'min' , minimalQuantity . toString ( ) ) ;
301+ qtyInput . setAttribute ( 'value' , minimalQuantity . toString ( ) ) ;
302+ }
303+ } ) ;
304+ } ;
305+
281306document . addEventListener ( 'DOMContentLoaded' , ( ) => {
282307 const { prestashop, Theme : { events, selectors} } = window ;
283308
309+ populateMinQuantityInput ( ) ;
310+
284311 prestashop . on ( events . updatedCart , ( ) => {
285312 useQuantityInput ( cartSelectorMap . productQuantity ) ;
286313
@@ -289,7 +316,10 @@ document.addEventListener('DOMContentLoaded', () => {
289316 cartOverview ?. focus ( ) ;
290317 } ) ;
291318
292- prestashop . on ( events . quickviewOpened , ( ) => useQuantityInput ( quantityInputMap . modal ) ) ;
319+ prestashop . on ( events . quickviewOpened , ( ) => {
320+ useQuantityInput ( quantityInputMap . modal ) ;
321+ populateMinQuantityInput ( quantityInputMap . modal ) ;
322+ } ) ;
293323} ) ;
294324
295325export default useQuantityInput ;
0 commit comments