Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: issue-616 and issue-615 #621

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/js/constants/selectors-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export const qtyInput = {
modal: '.modal-dialog .js-quantity-button',
increment: '.js-increment-button',
decrement: '.js-decrement-button',
quantityWanted: '.js-quantity-wanted',
confirm: '.confirmation',
icon: '.material-icons',
spinner: '.spinner-border',
Expand Down
43 changes: 43 additions & 0 deletions src/js/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,47 @@ export default () => {
initProductSlide();
prestashop.on(events.updatedProduct, initProductSlide);
prestashop.on(events.quickviewOpened, initProductSlide);

function detectQuantityChange() {
const quantityInput = document.querySelector(
SelectorsMap.qtyInput.quantityWanted,
) as HTMLInputElement;
const incrementButton = document.querySelector(
SelectorsMap.qtyInput.increment,
) as HTMLButtonElement;
const decrementButton = document.querySelector(
SelectorsMap.qtyInput.decrement,
) as HTMLButtonElement;

if (quantityInput && incrementButton && decrementButton) {
// Function to trigger emit
const triggerEmit = () => {
const inputValue = parseInt(quantityInput.value, 10);
const minValue = parseInt(quantityInput.min, 10);

// Check if the input value is a valid and greater or equal than the minimum value
if (!isNaN(inputValue) && inputValue >= minValue) {
quantityInput.value = inputValue.toString();
} else {
quantityInput.value = minValue.toString();
}

prestashop.emit('updateProduct', {
eventType: 'updatedProductQuantity',
});
};

// Attach event listener for input changes
quantityInput.addEventListener('input', triggerEmit);
quantityInput.addEventListener('keyup', triggerEmit);
quantityInput.addEventListener('keydown', triggerEmit);

// Attach event listener for increment / decrement button click
incrementButton.addEventListener('click', triggerEmit);
decrementButton.addEventListener('click', triggerEmit);
}
}

// Call the function to start listening for quantity changes
detectQuantityChange();
};
1 change: 1 addition & 0 deletions templates/catalog/_partials/product-add-to-cart.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
{include file='components/qty-input.tpl'
attributes=[
"id" => "quantity_wanted",
"class" => "form-control js-quantity-wanted",
"value" => "{$product.minimal_quantity}",
"min" => "{$product.minimal_quantity}"
]
Expand Down
Loading