【问题标题】:Update the number of products in cart (not quantity) - Shopify Mobile Buy SDK更新购物车中的产品数量(不是数量) - Shopify Mobile Buy SDK
【发布时间】: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,但无法得到我想要的

标签: ios swift shopify


【解决方案1】:

使用本地购物车。用户点击结帐后,使用 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),
])

)

以上代码取自 docs,分别为数量 5 和 3 的两个项目 mFyaWFU8vc2hGl 启动结帐。

【讨论】:

  • 是 mFyaWFU 和 8vc2hGl 变体还是产品 id(和 base64 编码)?
  • @AliSadeghipourKorabaslo .. 这应该是 Shopify 定义的 graphql 语法中的变体 ID
  • 我不使用 graphQL 并从 x:y@thehypeapp.myshopify.com/admin/api/2020-10/products/a/… 获取变体 ID,它以明文形式返回,而不是 base64。有什么方法可以将 id 转换为 graphQL id?我尝试转换 base64(gid://shopify/ProductVariant/[variantID]) 但不工作。
  • @AliSadeghipourKorabaslo 你试过这个吗? - shopify.dev/tutorials/migrate-from-rest-api-to-graphql
  • 啊,是的,我之前看到过,但这让我很困惑,因为 iOS Swift 中使用的语法与这里非常不同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-12
  • 2020-12-06
  • 1970-01-01
  • 1970-01-01
  • 2013-09-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多