Skip to content

Commit 6cb9cba

Browse files
[AC][SampleApp] Clear cart state after onComplete (avoid failed onClick) (#333)
fix: cart left over from success won't work
1 parent d8dfb45 commit 6cb9cba

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

Samples/ShopifyAcceleratedCheckoutsApp/ShopifyAcceleratedCheckoutsApp/Views/CartBuilderView.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ struct CartBuilderView: View {
6666

6767
ButtonSet(
6868
cart: $cart,
69-
firstVariantQuantity: cart.lines.nodes.first?.quantity ?? 1
69+
firstVariantQuantity: cart.lines.nodes.first?.quantity ?? 1,
70+
onComplete: clearCart
7071
)
7172
}
7273

@@ -103,14 +104,7 @@ struct CartBuilderView: View {
103104
isLoadingProducts: isLoadingProducts,
104105
hasProducts: !allProducts.isEmpty,
105106
onCreateCart: createCustomCart,
106-
onClearCart: {
107-
withAnimation {
108-
cart = nil
109-
selectedVariants.removeAll()
110-
}
111-
// Trigger scroll using state change
112-
scrollToTop = true
113-
}
107+
onClearCart: clearCart
114108
)
115109
.padding(.vertical, 12)
116110
.background(Color(UIColor.systemBackground))
@@ -139,6 +133,15 @@ struct CartBuilderView: View {
139133
}
140134
}
141135

136+
private func clearCart() {
137+
withAnimation {
138+
cart = nil
139+
selectedVariants.removeAll()
140+
}
141+
// Trigger scroll using state change
142+
scrollToTop = true
143+
}
144+
142145
func onLoad() async {
143146
// Prevent multiple simultaneous loads
144147
guard !isLoadingProducts else {

Samples/ShopifyAcceleratedCheckoutsApp/ShopifyAcceleratedCheckoutsApp/Views/Components/ButtonSet.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import SwiftUI
2828
struct ButtonSet: View {
2929
@Binding var cart: Cart?
3030
let firstVariantQuantity: Int
31+
let onComplete: () -> Void
3132

3233
var body: some View {
3334
VStack(spacing: 16) {
@@ -37,6 +38,7 @@ struct ButtonSet: View {
3738
AcceleratedCheckoutButtons(cartID: cartID)
3839
.onComplete {
3940
print("✅ Checkout completed successfully")
41+
onComplete()
4042
}
4143
.onFail {
4244
print("❌ Checkout failed")

0 commit comments

Comments
 (0)