Get Products by Location

Get Products by Location

Artur-Kedzior
Visitor
2 0 0

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 0

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