Skip to content

Commit

Permalink
fix: checkout refresh cart (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanilowicz authored Jun 27, 2023
1 parent f642814 commit 558c9d0
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-tips-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware-pwa/composables-next": minor
---

Remove refreshing cart from create order function
5 changes: 5 additions & 0 deletions .changeset/poor-squids-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vue-demo-store": minor
---

Checkout - refresh the cart after redirecting to the summary page
2 changes: 2 additions & 0 deletions apps/docs/src/getting-started/e-commerce/checkout.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,10 @@ After placing an order with the `createOrder` method, the cart is refreshed auto

```ts
const { createOrder } = useCheckout();
const { refreshCart } = useCart();

const order = await createOrder();
refreshCart();
```

After creating an order, you can fetch order data. `orderId` is returned by the `createOrder` method from the `useCheckout` composable.
Expand Down
1 change: 1 addition & 0 deletions apps/docs/src/getting-started/e-commerce/custom-payment.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ This event is called when the user approves the payment process. It's the last s
);
// createOrder from useCheckout composable
orderCreated.value = await createOrder();
refreshCart()
// apiInstance from useShopwareContext composable
const handlePaymentResponse = await apiInstance.invoke.post(
"/store-api/handle-payment",
Expand Down
1 change: 1 addition & 0 deletions apps/docs/src/getting-started/e-commerce/payments.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ To give an example, let's say we need to implement a payment method which redire

```js{3}
const { createOrder } = useCheckout();
const { refreshCart } = useCart();
// create an order
const order = await createOrder();
```
Expand Down
3 changes: 3 additions & 0 deletions examples/express-checkout/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
usePrice,
useShopwareContext,
useAddToCart,
useCart,
} from "@shopware-pwa/composables-next";
import {
getProductThumbnailUrl,
Expand All @@ -36,6 +37,7 @@ const { paymentMethods, getPaymentMethods, createOrder } = useCheckout();
const { setPaymentMethod } = useSessionContext();
const { apiInstance } = useShopwareContext();
const { addToCart } = useAddToCart(productFound);
const { refreshCart } = useCart();
const productName = computed(() =>
getTranslatedProperty(productFound.value, "name")
Expand Down Expand Up @@ -95,6 +97,7 @@ const renderPaypalButtons = async () => {
}
);
orderCreated.value = await createOrder();
refreshCart();
const handlePaymentResponse = await apiInstance.invoke.post(
"/store-api/handle-payment",
{
Expand Down
8 changes: 7 additions & 1 deletion examples/use-checkout/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<script setup lang="ts">
import { onMounted, ref, computed } from "vue";
import { useCheckout, useSessionContext } from "@shopware-pwa/composables-next";
import {
useCart,
useCheckout,
useSessionContext,
} from "@shopware-pwa/composables-next";
import { Error } from "@shopware-pwa/types";
const {
Expand All @@ -17,6 +21,7 @@ const {
selectedShippingMethod: shippingMethod,
refreshSessionContext,
} = useSessionContext();
const { refreshCart } = useCart();
const checkoutPaymentMethod = computed({
get: () => paymentMethod.value?.id,
Expand All @@ -33,6 +38,7 @@ const createOrderError = ref<Error[]>([]);
const createOrderProxy = async () => {
try {
const response = await createOrder();
refreshCart();
} catch (error) {
createOrderError.value = (error as any).messages || [];
}
Expand Down
2 changes: 0 additions & 2 deletions packages/composables/src/useCheckout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ export function useCheckout(): UseCheckoutReturn {
} catch (e) {
const err = e as ClientApiError;
throw err;
} finally {
refreshCart();
}
}

Expand Down
14 changes: 11 additions & 3 deletions templates/vue-demo-store/pages/checkout/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ const {
activeBillingAddress,
setActiveBillingAddress,
} = useSessionContext();
const { cart, cartItems, subtotal, totalPrice, shippingTotal, isVirtualCart } =
useCart();
const {
cart,
cartItems,
subtotal,
totalPrice,
shippingTotal,
isVirtualCart,
refreshCart,
} = useCart();
const { customerAddresses, loadCustomerAddresses } = useAddress();
const isLoading = reactive<{ [key: string]: boolean }>({});
Expand Down Expand Up @@ -181,7 +188,8 @@ const placeOrder = async () => {
isLoading["placeOrder"] = true;
const order = await createOrder();
isLoading["placeOrder"] = false;
return push("/checkout/success/" + order.id);
await push("/checkout/success/" + order.id);
refreshCart();
};
const termsSelected = computed(() => {
Expand Down

2 comments on commit 558c9d0

@vercel
Copy link

@vercel vercel bot commented on 558c9d0 Jun 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

frontends-demo – ./templates/vue-demo-store

frontends-demo-git-main-shopware-frontends.vercel.app
frontends-demo.vercel.app
frontends-demo-shopware-frontends.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 558c9d0 Jun 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.