0
votes

I'am developer an app using Shopify Mobile Buy SDK where I am facing issue in add product in cart.

I'am able to add a product in cart using the following code.

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()
}

The above code is working and a product is being added at a time in cart.

But when I add another product in cart then existing item is replaced with new one. It does not update the number of products in cart

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

I have tried using the following query.

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()
                }
            }
        }

But it is not updating the number of products in existing cart.

Any suggestions?

What am I doing wrong?

1
Aren't you resetting the cart everytime you use this? Storefront.CheckoutCreateInput.create(email: .value(email), lineItems: .value([ Storefront.CheckoutLineItemInput.create(quantity: qty, variantId: id) ])) - HymnZzy
Yes, I know this. but I did not find anything to update number of products in cart. - Mahendra
I also used Storefront.CheckoutLineItemInput but could not get what I want - Mahendra

1 Answers

0
votes

Use a local cart. Once the user clicks on checkout use the API to create a checkout for all items in the cart.

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),
])

)

The above code taken from docs initiates the checkout for two item mFyaWFU and 8vc2hGl for quantities 5 and 3 respectively.