【发布时间】:2019-08-29 06:50:03
【问题描述】:
我是一个使用 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()
}
上面的代码正在运行,并且一次将产品添加到购物车中。
但是当我在购物车中添加另一个产品时,现有项目会被新项目替换。它不会更新购物车中的产品数量
我想要什么
当购物车为空时 -> 客户在购物车中添加产品 -> 购物车中有 1 个产品
当客户在购物车中添加另一个产品时 -> 购物车应该包含 2 个产品
当客户在购物车中添加另一个产品时 -> 购物车应该包含 3 个产品
我已尝试使用以下查询。
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()
}
}
}
但它不会更新现有购物车中的产品数量。
有什么建议吗?
我做错了什么?
【问题讨论】:
-
你不是每次使用这个都重置购物车吗?
Storefront.CheckoutCreateInput.create(email: .value(email), lineItems: .value([ Storefront.CheckoutLineItemInput.create(quantity: qty, variantId: id) ])) -
是的,我知道这一点。但我没有找到任何可以更新购物车中产品数量的信息。
-
我也使用了
Storefront.CheckoutLineItemInput,但无法得到我想要的