Skip to content

Commit

Permalink
Merge pull request #17 from FosterCommerce/fixes-for-line-item-qty-ad…
Browse files Browse the repository at this point in the history
…justments

Fix for max qty check
  • Loading branch information
peteeveleigh authored Aug 21, 2024
2 parents ddfedcf + 0f6b882 commit e73aef6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/templates/_components/app/line-item-cart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

<button type="button"
class="flex justify-center items-center w-[38px] h-[38px] text-[var(--brandColor)]"
@click="decrement"
@click="remove"
>
<span v-cloak v-if="qty <= 1">
<span class="sr-only">{{ 'Remove from cart'|t('foster-checkout') }}</span>
Expand Down
79 changes: 43 additions & 36 deletions src/web/assets/checkout/dist/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,34 @@ const LineItem = (props) => {
this.qty = this.qty > 1 ? (this.qty - 1) : 1;
this.updateQty();
},
remove() {
RemoveLineItem();
async remove() {
const form = document.querySelector(`#lineItemQty-${props.id}`);
const formData = new FormData(form)
formData.set(`lineItems[${props.id}][remove]`, true);

await fetch('/actions/commerce/cart/update-cart', {
method: 'POST',
headers: {
'Accept': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
},
body: formData,
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
// we should only do this if the ajax operation was successful
const container = form.closest('article');
container.remove();
})
.catch(error => {
console.error('Error:', error);
});

},
blur() {
this.qty = this.qty === '' ? 0 : this.qty;
Expand All @@ -76,10 +102,10 @@ const LineItem = (props) => {
if(this.unlimitedStock === 0 && this.qty > this.stock) {
this.qty = this.stock;
this.showErrorStockMessage = true;
} else if(this.qty < this.min && this.min !== 0) {
} else if(this.qty < this.min && this.min != 0) {
this.qty = this.min;
this.showErrorMinMessage = true;
} else if(this.unlimitedStock && this.qty > this.max) {
} else if(this.unlimitedStock && this.max && this.qty > this.max) {
this.qty = this.max;
this.showErrorMaxMessage = true;
} else {
Expand All @@ -98,43 +124,24 @@ const LineItem = (props) => {
},
body: formData,
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
let item = data.cart.lineItems.filter((lineItem) => lineItem.id === props.id)
console.log(item);
this.lineSubtotal = item[0].subtotalAsCurrency;
this.line
})
.catch(error => {
console.error('Error:', error);
});
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
let item = data.cart.lineItems.filter((lineItem) => lineItem.id === props.id)
this.lineSubtotal = item[0].subtotalAsCurrency;
})
.catch(error => {
console.error('Error:', error);
});
}
}
}
};

// const UpdateQty = (props) => {
// const form = document.querySelector(`#lineItemQty-${props.id}`);
// const formData = new FormData(form)
// formData.set(`lineItems[${props.id}][qty]`, props.qty);
// UpdateCart(formData);
// }

const RemoveLineItem = (props) => {
const form = document.querySelector(`#lineItemQty-${props.id}`);
const formData = new FormData(form)
formData.set(`lineItems[${props.id}][remove]`, true);
UpdateCart(formData);
// we should only do this if the ajax operation was successful
const container = form.closest('article');
container.remove();
}


const FocusModal = (props) => {
return {
Expand Down

0 comments on commit e73aef6

Please sign in to comment.