Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to extract variant objects from cart? #1220

Open
Isuru-Nanayakkara opened this issue Nov 15, 2023 · 1 comment
Open

How to extract variant objects from cart? #1220

Isuru-Nanayakkara opened this issue Nov 15, 2023 · 1 comment

Comments

@Isuru-Nanayakkara
Copy link

Isuru-Nanayakkara commented Nov 15, 2023

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant