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: when you edit the product qty to zero, the product won't be removed from the cart #542

Merged
merged 4 commits into from
Sep 7, 2023
Merged
Changes from 1 commit
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
24 changes: 22 additions & 2 deletions src/js/pages/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ import handleCartAction from '../components/UseHandleCartAction';
export default () => {
const {Theme} = window;
const voucherCodes = document.querySelectorAll(Theme.selectors.cart.discountCode);
const cartContainer = document.querySelector<HTMLElement>(Theme.selectors.cart.container);

if (cartContainer) {
cartContainer.addEventListener('click', (event: Event) => {
const eventTarget = event.target as HTMLElement;

if (eventTarget.classList.contains('js-decrement-button')) {
const targetItem = eventTarget.closest('.cart__item');
const targetValue = targetItem?.querySelector('.js-cart-line-product-quantity') as HTMLElement | null;

if (targetValue && targetValue.getAttribute('value') === '1' && targetValue.getAttribute('min') === '1') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be careful the min quantity can also be 0 which should be handled like 1, meaning the feature is disabled there is no minimum
Except that I think the original behaviour from classic is consistent and should be used as a base as it's a common way to handle this in many e-commerce shops

tldr; let's stay iso functional

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah and one other comment you seem to be listening to the click event, so only the arrows clicking but the value can also be edited via keyboard, so you should either listen to the keydown event as well or maybe easier simply the change event?

Unless you need to listen to the specific event to cancel it when the value is not the minimum one?

if (targetItem) {
const removeButton = targetItem.querySelector('.remove-from-cart') as HTMLElement | null;

if (removeButton) {
removeButton.click();
}
}
}
}
});
}

voucherCodes.forEach((voucher) => {
voucher.addEventListener('click', (event: Event) => {
Expand All @@ -33,8 +55,6 @@ export default () => {
});
});

const cartContainer = document.querySelector<HTMLElement>(Theme.selectors.cart.container);

if (cartContainer) {
cartContainer.addEventListener('click', (event: Event) => {
const eventTarget = event.target as HTMLElement;
Expand Down
Loading