ganesh
ganesh

Reputation: 1237

Query Products in Shopify using Mobile-Buy-SDK Android


I have created a trial account in Shopify and added few products.I am trying to display those on my android app and purchase them. Those products are "News Article" kind of stuff and user can buy once and read them when they wish.

https://insightmy.myshopify.com/products.json (They have dummy contents, added for learning purpose only)

final ID id = new ID("72355741740");
Storefront.QueryRootQuery query2 = Storefront.query(new 
Storefront.QueryRootQueryDefinition() {
@Override
public void define(Storefront.QueryRootQuery _queryBuilder) {
    _queryBuilder.node(id, new Storefront.NodeQueryDefinition() {
        @Override
        public void define(NodeQuery _queryBuilder) {
            _queryBuilder.onProduct(new Storefront.ProductQueryDefinition() {
                @Override
                public void define(Storefront.ProductQuery _queryBuilder) {
                    _queryBuilder.title();
                }
            });
        }
    });
}

});

 QueryGraphCall call = cl.queryGraph(query2);
 call.enqueue(new GraphCall.Callback<Storefront.QueryRoot>() {
        @Override
        public void onResponse(@NonNull GraphResponse<Storefront.QueryRoot> response) {   
         ***//I could not cast this as shown in github***           
         //   Log.v("shop","boby : " + Storefront.Product)response.data()));
        }
        @Override
        public void onFailure(@NonNull GraphError error) {}});

I am not sure how to fetch the price and check if it is already purchased, If not, proceed to purchase.There is no detailed document on querying and retrieving methods.

Upvotes: 2

Views: 1576

Answers (1)

Duda
Duda

Reputation: 1683

You can use this query to get the product details and price:

            String productId = "your-product-id"
            Storefront.ProductQueryDefinition query = q -> q
            .title()
            .descriptionHtml()
            .tags()
            .collections(quw -> quw.first(1),
                    args -> args.edges(collectionEdge ->
                            collectionEdge.node(collectionQuery ->
                                    collectionQuery.title()
                            )
                    )
            )
            .images(args ->  args.first(250), imageConnection -> imageConnection
                    .edges(imageEdge -> imageEdge
                            .node(Storefront.ImageQuery::src)
                    )
            )
            .options(option -> option
                    .name()
                    .values()
            )
            .variants(args ->  args.first(250), variantConnection -> variantConnection
                    .edges(variantEdge -> variantEdge
                            .node(variant -> variant
                                    .title()
                                    .availableForSale()
                                    .image(Storefront.ImageQuery::src)
                                    .selectedOptions(selectedOption -> selectedOption
                                            .name()
                                            .value()
                                    )
                                    .price()
                            )
                    )
            );

    mGraphClient.queryGraph(Storefront.query(
            root -> root
                    .node(new ID(productId), node -> node
                            .onProduct(query)
                    )
            )
    ).enqueue(new GraphCall.Callback<Storefront.QueryRoot>() {
        @Override
        public void onResponse(@NonNull GraphResponse<Storefront.QueryRoot> response) {
            final Storefront.Product product = (Storefront.Product) response.data().getNode();
        }

        @Override
        public void onFailure(@NonNull GraphError error) {

        }
    });

If you want to know if user already purchased your product you'll need to save it on your server or create customer account for users who made purchase using the mobile pay sdk(here) and later you can fetch the user's order(here)

Upvotes: 1

Related Questions