Shopify / mobile-buy-sdk-ios

Shopify’s Mobile Buy SDK makes it simple to sell physical products inside your mobile app. With a few lines of code, you can connect your app with the Shopify platform and let your users buy your products using Apple Pay or their credit card.
MIT License
453 stars 198 forks source link

How to extract variant objects from cart? #1220

Open Isuru-Nanayakkara opened 9 months ago

Isuru-Nanayakkara commented 9 months ago

Hi,

I'm fetching a cart with the below query.

let query = Storefront.buildQuery { $0
    .cart(id: GraphQL.ID(rawValue: "")) { $0
        .id()
        .checkoutUrl()
        .lines(first: 1) { $0
            .edges { $0
                .node { $0
                    .id()
                    .quantity()
                    .merchandise { $0
                        .onProductVariant { $0
                            .id()
                            .title()
                            .image { $0
                                .url()
                            }
                            .price { $0
                                .amount()
                                .currencyCode()
                            }
                            .availableForSale()
                            .quantityAvailable()
                        }
                    }
                }
            }
        }
        .cost { $0
            .totalAmount { $0
                .amount()
                .currencyCode()
            }
            .subtotalAmount { $0
                .amount()
                .currencyCode()
            }
        }
    }
}

I'd like to grab the product variant details of line items.

let task = client.mutateGraphWith(mutation) { [self] response, error in
    if let response = response?.cartCreate, let cartResponse = response.cart {
        let lines = cartResponse.lines.edges.map { $0.node }
        for line in lines {
            // extract product variant details
        }
    }
}
task.resume()

However as you can see above, the line item node has a merchandise field and I don't know how to get variant data out of it.

It appears that the Merchandise object is actually a protocol so it has no properties.

Screenshot 2023-11-14 at 12 41 29 PM

So how can I get the variant data from this?

Isuru-Nanayakkara commented 9 months ago

Okay, posting this question in ChatGPT gave me the following code which actually works! However I'd still like to know if this indeed is the correct way to do this (because you know, ChatGPT's answers need to be taken with a grain of salt)

let task = client.mutateGraphWith(mutation) { [self] response, error in
    if let response = response?.cartCreate, let cartResponse = response.cart {
        let lines = cartResponse.lines.edges.map { $0.node }
        for line in lines {
            if let productVariant = line.merchandise as? Storefront.ProductVariant {
                // Access product variant details
                let variantId = productVariant.id
                let title = productVariant.title
                // Access other properties as needed
            }
        }
    }
}
task.resume()