ios - 更新购物车中的产品数量(而非数量) - Shopify Mobile Buy SDK

标签 ios swift shopify

我是一个使用 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

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 的两项 mFyaWFU8vc2hGl 的结账。

关于ios - 更新购物车中的产品数量(而非数量) - Shopify Mobile Buy SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57704541/

相关文章:

node.js - JSDoc 对象定义中的多行

ruby - Shopify Liquid 语法 - {%- assign [some_var] = [some_val] -%} 和 {% assign [some_var] = [some_val] %} 有什么区别

ios - 在 HMEventTrigger 启用触发器中显示错误

ios - ARKIT:使用 PanGesture 移动对象(正确方式)

swift - 我们如何在 kotlin mutlplatform 中处理 ios 和 android 平台的 api 响应?

ios - 快速禁用 UITextField 中的闪烁光标?

ios - 我可以将旧的 AVAudioPlayer 与新的 AVAudioEngine 一起使用吗?

ios - swift 4错误的json数组解析

ios - 函数签名特化在 swift 中崩溃

php - Shopify 自定义支付网关实现