Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Get Products by Location

Get Products by Location

Artur-Kedzior
Visitor
2 0 1

So here is our setup:

Products:

1 Apple
2 Orange
3 Banana

Locations:

1 Wareheouse 1 
2 Wareheouse 2

Inventory

Wareheouse 1 
 - Apples (available: 100)
 - Oranges (available: 0)
Wareheouse 2
 - Apples (available: 100)
 - Oranges (available: 100)
 - Banans (available: 100)


I would like to get the list of products and their stock passing locationId as parameter (REST API or GraphQL). 

Is this even possible? 

I got as far as doing this by querying Warehouse 1:

{
    products(first: 10) {
      edges {
        node {
          id
          title,
          variants (first: 5) {
            edges {
                node {
                    inventoryItem {
                        inventoryLevel( locationId:"gid://shopify/Location/1" ) {
                            available
                        }
                    }
                }
            }
          }
        }
      }
      pageInfo {
        hasNextPage
      }
    }
}


But that returns records I'm not interested in (Oranges that are out of stock and Bananas are not present in this Warehouse)

{
    "data": {
        "products": {
            "edges": [
                {
                    "node": {
                        "id": "gid://shopify/Product/1",
                        "title": "Apple",
                        "variants": {
                            "edges": [
                                {
                                    "node": {
                                        "inventoryItem": {
                                            "inventoryLevel": {
                                                "available": 100
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                },
                {
                    "node": {
                        "id": "gid://shopify/Product/2",
                        "title": "Orange",
                        "variants": {
                            "edges": [
                                {
                                    "node": {
                                        "inventoryItem": {
                                            "inventoryLevel": {
                                                "available": 0
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                },
                {
                    "node": {
                        "id": "gid://shopify/Product/3",
                        "title": "Banana",
                        "variants": {
                            "edges": [
                                {
                                    "node": {
                                        "inventoryItem": {
                                            "inventoryLevel": null
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            ],
            "pageInfo": {
                "hasNextPage": false
            }
        }
    },
}

 

This would be perfect only if I could apply the criteria below but I'm not sure how to.

inventoryLevel not null 
availability > 0 


Is this possible? 

 

Reply 1 (1)

Artur-Kedzior
Visitor
2 0 1

The way I managed to do it is:

products(first: 10) {
    edges {
        node {
            id
            title,
            variants(first: 10) {
                edges {
                    node {
                        inventoryItem {
                            sku
                            inventoryLevel(locationId: $id) {
                                available
                            }
                        }
                    }
                }
            }
        }
    }
}

 

where  $id is my locationId = 66666666666 (not global one)

and filtering out not available products (example in c#)

var productList = response.Data.Products.Edges
    .Where(x => x.Node.Variants.Edges.Any(y => y.Node.InventoryItem.InventoryLevel != null))
    .Select(x => new ProductsListByLocationItem
    (
        x.Node.Id,
        x.Node.Title,
        x.Node.Variants.Edges.Single().Node.InventoryItem.Sku,
        x.Node.Variants.Edges.Single().Node.InventoryItem.InventoryLevel.Available
));