Skip to content

Commit

Permalink
fix(composables): add shipping costs (#1580)
Browse files Browse the repository at this point in the history
* fix(composables): add shipping costs

* fix(composables): shipping calculation

* fix: changes after CR
  • Loading branch information
mdanilowicz authored Jan 15, 2025
1 parent e72f6cc commit a04aa8c
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changeset/calm-ligers-give.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions .changeset/honest-crabs-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vue-demo-store": patch
---

Display all shipping costs in the cart components `cart.vue`, `checkout/index.vue`
33 changes: 33 additions & 0 deletions packages/composables/src/useCart/useCart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
11 changes: 11 additions & 0 deletions packages/composables/src/useCart/useCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,14 @@ export type UseCartReturn = {
totalPrice: ComputedRef<number>;
/**
* Shipping price
*
* @deprecated Use `shippingCosts` instead
*/
shippingTotal: ComputedRef<number>;
/**
* Shipping costs
*/
shippingCosts: ComputedRef<Schemas["CartDelivery"][]>;
/**
* The total price of all cart items
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -297,6 +307,7 @@ export function useCartFunction(): UseCartReturn {
isEmpty,
isVirtualCart,
consumeCartErrors,
shippingCosts,
};
}

Expand Down
1 change: 1 addition & 0 deletions templates/vue-demo-store/i18n/de-DE/cart.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
1 change: 1 addition & 0 deletions templates/vue-demo-store/i18n/en-GB/cart.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
1 change: 1 addition & 0 deletions templates/vue-demo-store/i18n/pl-PL/cart.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
29 changes: 19 additions & 10 deletions templates/vue-demo-store/pages/checkout/cart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -44,15 +44,24 @@ const hasItems = computed(() => cartItems.value.length > 0);
/>
</div>

<div
class="flex py-4 border-b justify-between text-sm text-secondary-500"
>
<p>{{ $t("cart.shippingEstimate") }}</p>
<SharedPrice
:value="shippingTotal"
class="text-secondary-900 font-medium"
data-testid="cart-subtotal"
/>
<div class="py-4 text-sm text-secondary-500 border-b">
<div
class="py-1 flex justify-between"
v-for="shippingCost in shippingCosts"
:key="shippingCost.shippingMethod?.id ?? Math.random() * 100"
>
<p>{{ $t("cart.shippingCosts") }}</p>
<div
v-if="shippingCost.shippingCosts?.totalPrice"
class="flex text-secondary-900"
>
<SharedPrice
:value="shippingCost.shippingCosts.totalPrice"
class="text-secondary-900 font-medium"
data-testid="cart-shipping-cost"
/>
</div>
</div>
</div>

<div
Expand Down
22 changes: 20 additions & 2 deletions templates/vue-demo-store/pages/checkout/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const {
cartItems,
subtotal,
totalPrice,
shippingTotal,
shippingCosts,
isVirtualCart,
refreshCart,
} = useCart();
Expand Down Expand Up @@ -1014,6 +1014,24 @@ const beforeCreateOrderValidation = () => {
</div>

<div
class="py-1 flex justify-between text-sm text-secondary-500"
v-for="shippingCost in shippingCosts"
:key="shippingCost.shippingMethod?.id ?? Math.random() * 100"
>
<p>{{ $t("cart.shippingCosts") }}</p>
<div
v-if="shippingCost.shippingCosts?.totalPrice"
class="flex text-secondary-900"
>
<SharedPrice
:value="shippingCost.shippingCosts.totalPrice"
class="text-secondary-900 font-medium"
data-testid="cart-shipping-cost"
/>
</div>
</div>

<!-- <div
class="flex pb-4 border-b justify-between text-sm text-secondary-500"
>
<p>{{ $t("checkout.shippingEstimate") }}</p>
Expand All @@ -1022,7 +1040,7 @@ const beforeCreateOrderValidation = () => {
class="text-secondary-900 font-medium"
data-testid="cart-subtotal"
/>
</div>
</div> -->

<div class="flex justify-between text-secondary-900 font-medium">
<p>{{ $t("checkout.orderTotal") }}l</p>
Expand Down

0 comments on commit a04aa8c

Please sign in to comment.