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?
Storefront.CheckoutCreateInput.create(email: .value(email), lineItems: .value([ Storefront.CheckoutLineItemInput.create(quantity: qty, variantId: id) ]))
- HymnZzyStorefront.CheckoutLineItemInput
but could not get what I want - Mahendra