Skip to content

Commit

Permalink
Merge pull request #27 from FosterCommerce/fix/cart-update-values
Browse files Browse the repository at this point in the history
Fix cart page line item calculations
  • Loading branch information
pseudoclass authored Sep 15, 2024
2 parents 753ec90 + 1ad96af commit 58f1de0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 20 deletions.
71 changes: 52 additions & 19 deletions src/templates/_components/app/line-item-cart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@
{% endif %}

<article
v-scope="LineItem({ id: {{ lineItem.id }}, qty: {{ lineItem.qty }}, min: {{ minQty ?? 0 }}, max: {{ maxQty ?? 0 }}, stock: {{ stock }}, unlimitedStock: {{ unlimitedStock }}, lineSubtotal: '{{ lineSubtotal }}', showErrorMaxMessage: false, showErrorMinMessage: false, showErrorStockMessage: false })"
v-scope="LineItem({
id: {{ lineItem.id }},
qty: {{ lineItem.qty }},
min: {{ minQty ?? 0 }},
max: {{ maxQty ?? 0 }},
stock: {{ stock }},
unlimitedStock: {{ unlimitedStock }},
lineSubtotal: '{{ lineSubtotal }}',
showErrorMaxMessage: false,
showErrorMinMessage: false,
showErrorStockMessage: false
})"
class="flex justify-between items-stretch gap-5 p-5 bg-gray-200 rounded-xl"
>

Expand Down Expand Up @@ -65,9 +76,11 @@
{{ actionInput('commerce/cart/update-cart') }}
<div class="inline-flex bg-white border border-gray-300 rounded-lg overflow-hidden">

<button type="button"
class="flex justify-center items-center w-[38px] h-[38px] text-[var(--brandColor)]"
@click="decrement"
<button
type="button"
class="flex justify-center items-center w-[38px] h-[38px] text-[var(--brandColor)]"
:disabled="sending"
@click="decrement"
>
<span v-cloak v-if="qty <= 1">
<span class="sr-only">{{ 'Remove from cart'|t('foster-checkout') }}</span>
Expand All @@ -85,20 +98,24 @@
</button>

<label for="qty_{{ lineItem.id }}" class="sr-only">{{ 'Quantity'|t('foster-checkout') }}</label>
<input name="lineItems[{{ lineItem.id }}][qty]"
v-model="qty"
id="qty_{{ lineItem.id }}"
type="text"
inputmode="numeric"
pattern="[0-9]*"
class="w-[38px] h-[38px] px-0 text-center border-none"
@input="input"
@blur="blur"
<input
name="lineItems[{{ lineItem.id }}][qty]"
v-model="qty"
id="qty_{{ lineItem.id }}"
type="text"
inputmode="numeric"
pattern="[0-9]*"
class="w-[38px] h-[38px] px-0 text-center border-none"
:disabled="sending"
@input="input"
@blur="blur"
/>

<button type="button"
class="flex justify-center items-center w-[38px] h-[38px] text-[var(--brandColor)]"
@click="increment"
<button
type="button"
class="flex justify-center items-center w-[38px] h-[38px] text-[var(--brandColor)]"
:disabled="sending"
@click="increment"
>
<span class="sr-only">{{ 'Increment quantity'|t('foster-checkout') }}</span>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"
Expand All @@ -110,9 +127,24 @@

</div>

<button type="submit"
class="js-hide text-sm underline text-gray-500 hover:no-underline hover:text-gray-700">
Update
<p v-cloak v-show="sending" class="inline-flex justify-center items-center gap-1 text-sm text-gray-500">
<svg aria-hidden="true" role="status" class="inline-block w-4 h-4 me-3 text-gray-300 animate-spin" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/>
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="#000000" />
</svg>
<span v-if="action === 'remove'" class="inline-block">
{{ 'Removing'|t('foster-checkout') }}
</span>
<span v-else class="inline-block">
{{ 'Updating'|t('foster-checkout') }}
</span>
</p>

<button
type="submit"
class="js-hide text-sm underline text-gray-500 hover:no-underline hover:text-gray-700"
>
{{ 'Update'|t('foster-checkout') }}
</button>

<div class="relative w-full w-full">
Expand Down Expand Up @@ -171,6 +203,7 @@
<button
type="button"
class="inline-flex justify-start items-center gap-2 text-sm underline text-gray-500 hover:no-underline hover:text-gray-700"
:disabled="sending"
@click="remove"
>
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="14" viewBox="0 0 12 14"
Expand Down
2 changes: 2 additions & 0 deletions src/translations/en/foster-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
'Postal Code' => 'Postal Code',
'Delivery Postal Code' => 'Delivery Postal Code',
'Update' => 'Update',
'Updating' => 'Updating',
'Removing' => 'Removing',
'Close' => 'Close',
'Your postal code helps us provide the most accurate delivery information.' => 'Your postal code helps us provide the most accurate delivery information.',
'Order Summary' => 'Order Summary',
Expand Down
16 changes: 15 additions & 1 deletion src/web/assets/checkout/dist/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ const LineItem = (props) => {
showErrorMaxMessage: props.showErrorMaxMessage,
showErrorMinMessage: props.showErrorMinMessage,
showErrorStockMessage: props.showErrorStockMessage,
action: '',
sending: false,
input() {
this.qty = this.qty.replace(/\D/g,'');
this.updateQty();
},
increment() {
this.removeMessages()
this.removeMessages();
this.qty++;
this.updateQty();
},
Expand All @@ -70,6 +72,8 @@ const LineItem = (props) => {
const form = document.querySelector(`#lineItemQty-${props.id}`);
const formData = new FormData(form)
formData.set(`lineItems[${props.id}][remove]`, true);
this.action = 'remove';
this.sending = true;

await fetch('/actions/commerce/cart/update-cart', {
method: 'POST',
Expand All @@ -86,9 +90,13 @@ const LineItem = (props) => {
return response.json();
})
.then(data => {
// TEMP: Reloading here to refresh the cart page instead of updating the data via ajax
location.reload();
/*
// we should only do this if the ajax operation was successful
const container = form.closest('article');
container.remove();
*/
})
.catch(error => {
console.error('Error:', error);
Expand Down Expand Up @@ -120,6 +128,8 @@ const LineItem = (props) => {
const form = document.querySelector(`#lineItemQty-${props.id}`);
const formData = new FormData(form)
formData.set(`lineItems[${props.id}][qty]`, props.qty);
this.action = 'update';
this.sending = true;

await fetch('/actions/commerce/cart/update-cart', {
method: 'POST',
Expand All @@ -136,8 +146,12 @@ const LineItem = (props) => {
return response.json();
})
.then(data => {
// TEMP: Reloading here to refresh the cart page instead of updating the data via ajax
location.reload();
/*
let item = data.cart.lineItems.filter((lineItem) => lineItem.id === props.id)
this.lineSubtotal = item[0].subtotalAsCurrency;
*/
})
.catch(error => {
console.error('Error:', error);
Expand Down

0 comments on commit 58f1de0

Please sign in to comment.