Skip to content

Commit 0573a3a

Browse files
committed
fix: m2-717. total price and discount calculation
1 parent 1742f45 commit 0573a3a

File tree

4 files changed

+43
-23
lines changed

4 files changed

+43
-23
lines changed

packages/theme/components/CartSidebar.vue

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,37 @@
202202
<transition name="sf-fade">
203203
<div v-if="totalItems">
204204
<SfProperty
205-
:name="$t('Subtotal price')"
206-
class="sf-property--full-width sf-property--large my-cart__total-price"
205+
v-if="totals.subtotal !== totals.total"
206+
:name="$t('Subtotal')"
207+
class="sf-property--full-width sf-property--small"
207208
>
208209
<template #value>
209210
<SfPrice
210211
:regular="$fc(totals.subtotal)"
211-
:special="
212-
totals.subtotal <= totals.special ? '' : $fc(totals.special)
213-
"
212+
class="my-cart__subtotal-price"
213+
/>
214+
</template>
215+
</SfProperty>
216+
<SfProperty
217+
v-if="discount"
218+
:name="$t('Discount')"
219+
class="sf-property--full-width sf-property--small"
220+
>
221+
<template #value>
222+
<SfPrice
223+
:regular="$fc(discount)"
224+
class="my-cart__discount"
225+
/>
226+
</template>
227+
</SfProperty>
228+
<hr class="sf-divider">
229+
<SfProperty
230+
:name="$t('Order Total')"
231+
class="sf-property--full-width sf-property--large my-cart__total-price"
232+
>
233+
<template #value>
234+
<SfPrice
235+
:regular="$fc(totals.total)"
214236
/>
215237
</template>
216238
</SfProperty>
@@ -322,6 +344,7 @@ export default defineComponent({
322344
},
323345
})));
324346
const totals = computed(() => cartGetters.getTotals(cart.value));
347+
const discount = computed(() => -cartGetters.getDiscountAmount(cart.value));
325348
const totalItems = computed(() => cartGetters.getTotalItems(cart.value));
326349
const getAttributes = (product: ConfigurableCartItem) => product.configurable_options || [];
327350
const getBundles = (product: BundleCartItem) => product.bundle_options?.map((b) => b.values).flat() || [];
@@ -393,6 +416,7 @@ export default defineComponent({
393416
isInStock,
394417
imageSizes,
395418
getMagentoImage,
419+
discount,
396420
};
397421
},
398422
});
@@ -451,10 +475,14 @@ export default defineComponent({
451475
margin: 0;
452476
}
453477
478+
&__subtotal, &__discount {
479+
--price-font-weight: var(--font-weight--light);
480+
}
481+
454482
&__total-price {
455-
--price-font-size: var(--font-size--xl);
483+
--price-font-size: var(--font-size--lg);
456484
--price-font-weight: var(--font-weight--medium);
457-
margin: 0 0 var(--spacer-base) 0;
485+
margin: var(--spacer-base) 0 var(--spacer-base) 0;
458486
}
459487
}
460488

packages/theme/components/Checkout/CartPreview.vue

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<SfProperty
2222
v-if="hasDiscounts"
2323
:name="$t('Discount')"
24-
:value="$fc(discountsAmount)"
24+
:value="$fc(discount)"
2525
class="sf-property--full-width sf-property--small property"
2626
/>
2727
<SfProperty
@@ -92,17 +92,13 @@ export default defineComponent({
9292
const products = computed(() => cartGetters.getItems(cart.value));
9393
const totalItems = computed(() => cartGetters.getTotalItems(cart.value));
9494
const totals = computed(() => cartGetters.getTotals(cart.value));
95-
const discounts = computed(() => cartGetters.getDiscounts(cart.value));
96-
const hasDiscounts = computed(() => discounts.value.length > 0);
97-
const discountsAmount = computed(
98-
() => -1 * discounts.value.reduce((a, el) => el.value + a, 0),
99-
);
95+
const discount = computed(() => -cartGetters.getDiscountAmount(cart.value));
96+
const hasDiscounts = computed(() => Math.abs(discount.value) > 0);
10097
const selectedShippingMethod = computed(() => cartGetters.getSelectedShippingMethod(cart.value));
10198
10299
return {
103100
cart,
104-
discounts,
105-
discountsAmount,
101+
discount,
106102
hasDiscounts,
107103
totalItems,
108104
listIsHidden,

packages/theme/getters/types.d.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,6 @@ export interface UserShippingGetters<USER_SHIPPING, USER_SHIPPING_ITEM> {
6767
isDefault: (address: USER_SHIPPING_ITEM) => boolean;
6868
}
6969

70-
export interface UserGetters<USER> {
71-
getFirstName: (customer: USER) => string;
72-
getLastName: (customer: USER) => string;
73-
getFullName: (customer: USER) => string;
74-
getEmailAddress: (customer: USER) => string;
75-
[getterName: string]: (element: any, options?: any) => unknown;
76-
}
77-
7870
export interface WishlistGetters<WISHLIST, WISHLIST_ITEM> {
7971
getItems: (wishlist: WISHLIST) => WISHLIST_ITEM[];
8072
getItemName: (wishlistItem: WISHLIST_ITEM) => string;
@@ -101,5 +93,6 @@ export interface CartGetters<CART, CART_ITEM> {
10193
// @deprecated - use getDiscounts instead
10294
getCoupons: (cart: CART) => Coupon[];
10395
getDiscounts: (cart: CART) => CartDiscount[];
96+
getDiscountAmount: (cart: CART) => number;
10497
[getterName: string]: (element: any, options?: any) => unknown;
10598
}

packages/theme/modules/checkout/getters/cartGetters.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ export const getDiscounts = (cart: Cart): CartDiscount[] => (Array.isArray(cart?
146146
code: d.label,
147147
} as CartDiscount)) : []);
148148

149+
export const getDiscountAmount = (cart: Cart): number => calculateDiscounts(cart?.prices?.discounts ?? []);
150+
149151
export const getAppliedCoupon = (cart: Cart): Coupon | null => (Array.isArray(cart?.applied_coupons) && cart?.applied_coupons.length > 0 ? {
150152
id: cart.applied_coupons[0].code,
151153
name: cart.applied_coupons[0].code,
@@ -189,6 +191,7 @@ const cartGetters: CartGetters = {
189191
getShippingPrice,
190192
getTotalItems,
191193
getTotals,
194+
getDiscountAmount,
192195
productHasSpecialPrice,
193196
getStockStatus,
194197
getConfiguredVariant,

0 commit comments

Comments
 (0)