我是一个使用 Shopify Mobile Buy SDK 的应用程序开发者,在将产品添加到购物车时遇到问题。
我可以使用以下代码在购物车中添加产品。
func addProductInCartWith(variantId id: GraphQL.ID, qty: Int32, handler: @escaping ((Storefront.Checkout?, String?)->Void)) {
let input = Storefront.CheckoutCreateInput.create(email: .value(email), lineItems: .value([
Storefront.CheckoutLineItemInput.create(quantity: qty, variantId: id)
]))
let mutation = Storefront.buildMutation { $0
.checkoutCreate(input: input) { $0
.checkout { $0
.id()
}
.checkoutUserErrors { $0
.field()
.message()
}
}
}
client.mutateGraphWith(mutation) { (response, error) in
Loader.shared.hide()
if let result = response, error == nil {
print("createCustomerCheckout response =\(result)")
handler(result.checkoutCreate?.checkout, result.checkoutCreate?.checkoutUserErrors.first?.message)
} else {
if let error = error, case .invalidQuery(let reason) = error {
handler(nil, reason.first?.message)
} else {
handler(nil, "Please try again later")
}
}
}.resume()
}
上面的代码正在运行,并且一次将产品添加到购物车中。
但是当我在购物车中添加另一种产品时,现有商品将被新商品替换。它不会更新购物车中的产品数量
What I want
block 引用>When cart is empty -> Customer add product in cart -> 1 product in cart
When customer add another product in cart -> cart should contain 2 product
When customer add another product in cart -> cart should contain 3 product
我尝试使用以下查询。
let input = Storefront.CheckoutLineItemInput.create(quantity: qty, variantId: id) let mutation = Storefront.buildMutation { $0 .checkoutLineItemsReplace(lineItems: [input], checkoutId: GraphQL.ID(rawValue: checkoutId)) { $0 .checkout { $0 .id() } .userErrors { $0 .field() .message() } } }
但它不会更新现有购物车中的产品数量。
有什么建议吗?
我做错了什么?
最佳答案
使用本地购物车。用户点击结帐后,使用 API 为购物车中的所有商品创建结帐。
let input = Storefront.CheckoutCreateInput.create(
lineItems: .value([
Storefront.CheckoutLineItemInput.create(variantId: GraphQL.ID(rawValue: "mFyaWFu"), quantity: 5),
Storefront.CheckoutLineItemInput.create(variantId: GraphQL.ID(rawValue: "8vc2hGl"), quantity: 3),
])
)
以上代码取自文档,分别启动数量为 5 和 3 的两项 mFyaWFU
和 8vc2hGl
的结账。
关于ios - 更新购物车中的产品数量(而非数量) - Shopify Mobile Buy SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57704541/