Skip to content

Commit 2ef39b2

Browse files
authored
Merge pull request #621 from tblivet/issue-616-615
Fix: issue-616 and issue-615
2 parents 1516c35 + f5bfd59 commit 2ef39b2

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/js/constants/selectors-map.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export const qtyInput = {
115115
modal: '.modal-dialog .js-quantity-button',
116116
increment: '.js-increment-button',
117117
decrement: '.js-decrement-button',
118+
quantityWanted: '.js-quantity-wanted',
118119
confirm: '.confirmation',
119120
icon: '.material-icons',
120121
spinner: '.spinner-border',

src/js/product.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,47 @@ export default () => {
2222
initProductSlide();
2323
prestashop.on(events.updatedProduct, initProductSlide);
2424
prestashop.on(events.quickviewOpened, initProductSlide);
25+
26+
function detectQuantityChange() {
27+
const quantityInput = document.querySelector(
28+
SelectorsMap.qtyInput.quantityWanted,
29+
) as HTMLInputElement;
30+
const incrementButton = document.querySelector(
31+
SelectorsMap.qtyInput.increment,
32+
) as HTMLButtonElement;
33+
const decrementButton = document.querySelector(
34+
SelectorsMap.qtyInput.decrement,
35+
) as HTMLButtonElement;
36+
37+
if (quantityInput && incrementButton && decrementButton) {
38+
// Function to trigger emit
39+
const triggerEmit = () => {
40+
const inputValue = parseInt(quantityInput.value, 10);
41+
const minValue = parseInt(quantityInput.min, 10);
42+
43+
// Check if the input value is a valid and greater or equal than the minimum value
44+
if (!isNaN(inputValue) && inputValue >= minValue) {
45+
quantityInput.value = inputValue.toString();
46+
} else {
47+
quantityInput.value = minValue.toString();
48+
}
49+
50+
prestashop.emit('updateProduct', {
51+
eventType: 'updatedProductQuantity',
52+
});
53+
};
54+
55+
// Attach event listener for input changes
56+
quantityInput.addEventListener('input', triggerEmit);
57+
quantityInput.addEventListener('keyup', triggerEmit);
58+
quantityInput.addEventListener('keydown', triggerEmit);
59+
60+
// Attach event listener for increment / decrement button click
61+
incrementButton.addEventListener('click', triggerEmit);
62+
decrementButton.addEventListener('click', triggerEmit);
63+
}
64+
}
65+
66+
// Call the function to start listening for quantity changes
67+
detectQuantityChange();
2568
};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
{include file='components/qty-input.tpl'
4646
attributes=[
4747
"id" => "quantity_wanted",
48+
"class" => "form-control js-quantity-wanted",
4849
"value" => "{$product.minimal_quantity}",
4950
"min" => "{$product.minimal_quantity}"
5051
]

0 commit comments

Comments
 (0)