From 927a3b25d0e85ace04896648449fa94fd6849a1c Mon Sep 17 00:00:00 2001 From: Dominic Klein Date: Tue, 9 Feb 2021 21:02:21 +0000 Subject: [PATCH] fix(default-theme): added shipping cost to order summary (#1357) --- api/composables.api.md | 2 + .../resources/api/composables.iusecart.md | 1 + .../api/composables.iusecart.shippingtotal.md | 14 +++++++ .../composables/__tests__/useCart.spec.ts | 38 +++++++++++++++++++ .../composables/src/hooks/useCart/index.ts | 8 ++++ .../checkout/sidebar/SidebarOrderSummary.vue | 12 ++---- .../checkout/summary/TotalsSummary.vue | 4 +- 7 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 docs/landing/resources/api/composables.iusecart.shippingtotal.md diff --git a/api/composables.api.md b/api/composables.api.md index 0cc34bbd4..6e07c23c2 100644 --- a/api/composables.api.md +++ b/api/composables.api.md @@ -217,6 +217,8 @@ export interface IUseCart { // @deprecated (undocumented) removeProduct: ({ id }: Partial) => void; // (undocumented) + shippingTotal: ComputedRef; + // (undocumented) subtotal: ComputedRef; // (undocumented) totalPrice: ComputedRef; diff --git a/docs/landing/resources/api/composables.iusecart.md b/docs/landing/resources/api/composables.iusecart.md index cf5dab8e0..dddbd6239 100644 --- a/docs/landing/resources/api/composables.iusecart.md +++ b/docs/landing/resources/api/composables.iusecart.md @@ -31,6 +31,7 @@ export interface IUseCart | [refreshCart](./composables.iusecart.refreshcart.md) | () => void | (BETA) | | [removeItem](./composables.iusecart.removeitem.md) | ({ id }: LineItem) => Promise<void> | (BETA) | | [removeProduct](./composables.iusecart.removeproduct.md) | ({ id }: Partial<Product>) => void | (BETA) | +| [shippingTotal](./composables.iusecart.shippingtotal.md) | ComputedRef<number> | (BETA) | | [subtotal](./composables.iusecart.subtotal.md) | ComputedRef<number> | (BETA) | | [totalPrice](./composables.iusecart.totalprice.md) | ComputedRef<number> | (BETA) | diff --git a/docs/landing/resources/api/composables.iusecart.shippingtotal.md b/docs/landing/resources/api/composables.iusecart.shippingtotal.md new file mode 100644 index 000000000..c167c5dd8 --- /dev/null +++ b/docs/landing/resources/api/composables.iusecart.shippingtotal.md @@ -0,0 +1,14 @@ + + +[Home](./index.md) > [@shopware-pwa/composables](./composables.md) > [IUseCart](./composables.iusecart.md) > [shippingTotal](./composables.iusecart.shippingtotal.md) + +## IUseCart.shippingTotal property + +> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. +> + +Signature: + +```typescript +shippingTotal: ComputedRef; +``` diff --git a/packages/composables/__tests__/useCart.spec.ts b/packages/composables/__tests__/useCart.spec.ts index 715d4e461..cac6598d9 100644 --- a/packages/composables/__tests__/useCart.spec.ts +++ b/packages/composables/__tests__/useCart.spec.ts @@ -42,6 +42,44 @@ describe("Composables - useCart", () => { stateUser.value = null; }); describe("computed", () => { + describe("shippingTotal", () => { + it("should return default value 0 (zero) if cart is empty", () => { + stateCart.value = undefined as any; + const { shippingTotal } = useCart(rootContextMock); + expect(shippingTotal.value).toBe(0); + }); + it("should return default value 0 (zero) if there is no delivery in cart", () => { + stateCart.value = { + deliveries: undefined, + }; + const { shippingTotal } = useCart(rootContextMock); + expect(shippingTotal.value).toBe(0); + }); + it("should return default value 0 (zero) if shipping costs are empty", () => { + stateCart.value = { + deliveries: [ + { + shippingCosts: undefined, + }, + ], + }; + const { shippingTotal } = useCart(rootContextMock); + expect(shippingTotal.value).toBe(0); + }); + it("should return total price from shipping cost of the first delivery from cart", () => { + stateCart.value = { + deliveries: [ + { + shippingCosts: { + totalPrice: 199.5, + }, + }, + ], + }; + const { shippingTotal } = useCart(rootContextMock); + expect(shippingTotal.value).toBe(199.5); + }); + }); describe("cart", () => { it("should be null on not loaded cart", () => { stateCart.value = null; diff --git a/packages/composables/src/hooks/useCart/index.ts b/packages/composables/src/hooks/useCart/index.ts index 5a4d1497c..6bebf936b 100644 --- a/packages/composables/src/hooks/useCart/index.ts +++ b/packages/composables/src/hooks/useCart/index.ts @@ -52,6 +52,7 @@ export interface IUseCart { */ removeProduct: ({ id }: Partial) => void; totalPrice: ComputedRef; + shippingTotal: ComputedRef; subtotal: ComputedRef; } @@ -153,6 +154,12 @@ export const useCart = (rootContext: ApplicationVueContext): IUseCart => { return cartPrice || 0; }); + const shippingTotal = computed(() => { + const shippingTotal = + cart.value?.deliveries?.[0]?.shippingCosts?.totalPrice; + return shippingTotal || 0; + }); + const subtotal = computed(() => { const cartPrice = cart.value?.price?.positionPrice; return cartPrice || 0; @@ -172,6 +179,7 @@ export const useCart = (rootContext: ApplicationVueContext): IUseCart => { removeProduct, removeItem, totalPrice, + shippingTotal, subtotal, }; }; diff --git a/packages/default-theme/src/components/checkout/sidebar/SidebarOrderSummary.vue b/packages/default-theme/src/components/checkout/sidebar/SidebarOrderSummary.vue index 8f00aea24..189193312 100644 --- a/packages/default-theme/src/components/checkout/sidebar/SidebarOrderSummary.vue +++ b/packages/default-theme/src/components/checkout/sidebar/SidebarOrderSummary.vue @@ -17,7 +17,7 @@ /> @@ -59,17 +59,13 @@ export default { SwPromoCode, }, setup(props, { root }) { - const { count, totalPrice, subtotal } = useCart(root) + const { count, subtotal, shippingTotal, totalPrice } = useCart(root) - // TODO: use useSessionContext - const shippingMethod = { - price: "TODO: add price", - } return { count, - totalPrice, subtotal, - shippingMethod, + shippingTotal, + totalPrice, } }, data() { diff --git a/packages/default-theme/src/components/checkout/summary/TotalsSummary.vue b/packages/default-theme/src/components/checkout/summary/TotalsSummary.vue index 7612cb075..e9e14e564 100644 --- a/packages/default-theme/src/components/checkout/summary/TotalsSummary.vue +++ b/packages/default-theme/src/components/checkout/summary/TotalsSummary.vue @@ -1,6 +1,6 @@