diff --git a/.changeset/calm-ligers-give.md b/.changeset/calm-ligers-give.md new file mode 100644 index 000000000..48f844f76 --- /dev/null +++ b/.changeset/calm-ligers-give.md @@ -0,0 +1,6 @@ +--- +"@shopware-pwa/composables-next": patch +--- + +Added `shippingCosts` property in `useCart` composable that returns shipping costs of the cart, with the shipping discounts. +`shippingTotal` function is now deprecated as it only returns the first value from the array. The backend is returning a collection. diff --git a/.changeset/honest-crabs-march.md b/.changeset/honest-crabs-march.md new file mode 100644 index 000000000..a67eea39a --- /dev/null +++ b/.changeset/honest-crabs-march.md @@ -0,0 +1,5 @@ +--- +"vue-demo-store": patch +--- + +Display all shipping costs in the cart components `cart.vue`, `checkout/index.vue` diff --git a/packages/composables/src/useCart/useCart.test.ts b/packages/composables/src/useCart/useCart.test.ts index aa2193582..32adac434 100644 --- a/packages/composables/src/useCart/useCart.test.ts +++ b/packages/composables/src/useCart/useCart.test.ts @@ -215,4 +215,37 @@ describe("useCart", () => { expect(vm.consumeCartErrors()).toEqual(null); }); + + it("should return shipping costs", async () => { + injections.apiClient.invoke.mockResolvedValue({ + data: { + deliveries: [ + { + shippingMethod: { + id: "a9d9cc502b3547f4a89eb2830c032c78", + }, + }, + { + shippingMethod: { + id: "a9d9cc502b3547f4a89eb2830c032c78", + }, + }, + ], + }, + }); + await vm.refreshCart(); + + expect(vm.shippingCosts.length).toEqual(2); + }); + + it("should return empty shipping costs", async () => { + injections.apiClient.invoke.mockResolvedValue({ + data: { + deliveries: null, + }, + }); + await vm.refreshCart(); + + expect(vm.shippingCosts.length).toEqual(0); + }); }); diff --git a/packages/composables/src/useCart/useCart.ts b/packages/composables/src/useCart/useCart.ts index ee7c0ce02..5d988ca6d 100644 --- a/packages/composables/src/useCart/useCart.ts +++ b/packages/composables/src/useCart/useCart.ts @@ -66,8 +66,14 @@ export type UseCartReturn = { totalPrice: ComputedRef; /** * Shipping price + * + * @deprecated Use `shippingCosts` instead */ shippingTotal: ComputedRef; + /** + * Shipping costs + */ + shippingCosts: ComputedRef; /** * The total price of all cart items */ @@ -239,6 +245,10 @@ export function useCartFunction(): UseCartReturn { return shippingTotal || 0; }); + const shippingCosts = computed(() => { + return cart.value?.deliveries || []; + }); + const subtotal = computed(() => { const cartPrice = cart.value?.price?.positionPrice; return cartPrice || 0; @@ -297,6 +307,7 @@ export function useCartFunction(): UseCartReturn { isEmpty, isVirtualCart, consumeCartErrors, + shippingCosts, }; } diff --git a/templates/vue-demo-store/i18n/de-DE/cart.json b/templates/vue-demo-store/i18n/de-DE/cart.json index fe5e6b47e..eb9a8aab5 100644 --- a/templates/vue-demo-store/i18n/de-DE/cart.json +++ b/templates/vue-demo-store/i18n/de-DE/cart.json @@ -8,6 +8,7 @@ "orderTotal": "Gesamtbestellwert", "subtotal": "Zwischensumme", "shippingEstimate": "Versandkostenabschätzung", + "shippingCosts": "Versandkosten", "emptyCartLabel": "Ihr Warenkorb ist leer!", "messages": { "addedToCart": "{p} wurde dem Warenkorb hinzugefügt." diff --git a/templates/vue-demo-store/i18n/en-GB/cart.json b/templates/vue-demo-store/i18n/en-GB/cart.json index dcbe38325..a1ad5a467 100644 --- a/templates/vue-demo-store/i18n/en-GB/cart.json +++ b/templates/vue-demo-store/i18n/en-GB/cart.json @@ -8,6 +8,7 @@ "orderTotal": "Order total", "subtotal": "Subtotal", "shippingEstimate": "Shipping estimate", + "shippingCosts": "Shipping costs", "emptyCartLabel": "Your cart is empty!", "messages": { "addedToCart": "{p} has been added to cart." diff --git a/templates/vue-demo-store/i18n/pl-PL/cart.json b/templates/vue-demo-store/i18n/pl-PL/cart.json index eab965cfc..81691b9df 100644 --- a/templates/vue-demo-store/i18n/pl-PL/cart.json +++ b/templates/vue-demo-store/i18n/pl-PL/cart.json @@ -8,6 +8,7 @@ "orderTotal": "Całkowity koszt zamówienia", "subtotal": "Suma częściowa", "shippingEstimate": "Szacowana wysyłka", + "shippingCosts": "Koszty wysyłki", "emptyCartLabel": "Twój koszyk jest pusty!", "messages": { "addedToCart": "{p} został dodany do koszyka." diff --git a/templates/vue-demo-store/pages/checkout/cart.vue b/templates/vue-demo-store/pages/checkout/cart.vue index 01a6ccef9..feceb7765 100644 --- a/templates/vue-demo-store/pages/checkout/cart.vue +++ b/templates/vue-demo-store/pages/checkout/cart.vue @@ -4,7 +4,7 @@ defineOptions({ }); definePageMeta({ layout: "checkout" }); -const { cartItems, subtotal, totalPrice, shippingTotal } = useCart(); +const { cartItems, subtotal, totalPrice, shippingCosts } = useCart(); const localePath = useLocalePath(); const { formatLink } = useInternationalization(localePath); const hasItems = computed(() => cartItems.value.length > 0); @@ -44,15 +44,24 @@ const hasItems = computed(() => cartItems.value.length > 0); /> -
-

{{ $t("cart.shippingEstimate") }}

- +
+
+

{{ $t("cart.shippingCosts") }}

+
+ +
+
{
+

{{ $t("cart.shippingCosts") }}

+
+ +
+
+ +

{{ $t("checkout.orderTotal") }}l