Skip to content

Commit 2747b27

Browse files
committed
Fix minimum qty input on products listing and product detail pages
1 parent 898132d commit 2747b27

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

src/js/components/useQuantityInput.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
281306
document.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

295325
export default useQuantityInput;

src/js/constants/selectors-map.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export const desktopMenu = {
109109

110110
export const qtyInput = {
111111
default: '.js-quantity-button',
112+
idProductInput: 'input[name="id_product"]',
112113
modal: '.modal-dialog .js-quantity-button',
113114
increment: '.js-increment-button',
114115
decrement: '.js-decrement-button',

templates/catalog/_partials/miniatures/product.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@
186186
{include file='components/qty-input.tpl'
187187
attributes=[
188188
"id"=>"quantity_wanted_{$product.id_product}",
189-
"value"=>"{if $product.cart_quantity && $product.cart_quantity >= $product.minimal_quantity}1{else}{$product.minimal_quantity}{/if}",
190-
"min"=>"{if $product.cart_quantity && $product.cart_quantity >= $product.minimal_quantity}1{else}{$product.minimal_quantity}{/if}"
189+
"value"=>"1",
190+
"data-min"=>"{$product.minimal_quantity}"
191191
]
192192
marginHelper="mb-0"
193193
}

templates/catalog/_partials/product-add-to-cart.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
{include file='components/qty-input.tpl'
4646
attributes=[
4747
"id"=>"quantity_wanted",
48-
"value"=>"{if $product.quantity_wanted}{$product.quantity_wanted}{else}1{/if}",
49-
"min"=>"{if $product.quantity_wanted}{$product.minimal_quantity}{else}1{/if}"
48+
"value"=>"1",
49+
"data-min"=>"{$product.minimal_quantity}"
5050
]
5151
}
5252
</div>

0 commit comments

Comments
 (0)