skip to Main Content

Trying to set the available inventory for a large amount of products within multiple locations.

The problem I am running into is there is just so many products that I need to find their inventory_item_id in order to utilize the inventoryActivate mutation.

How can I grab a list of ALL of my 9,000 products inventory_item_id without going one-by-one?

Use Case:

  • 10 Locations used in Shopify
  • 9,000 Products in Shopify
  • ERP creates and holds the physical inventory of each
  • Need to update the Inventory of each Product at its respective location every hour or so

I am looking at the docs found here.

2

Answers


  1. Chosen as BEST ANSWER

    I have found that we can utilize the bulkOperationRunQuery following the docs here.

    1. I first ran the following to create the bulk operation with sku and inventoryItem:

    mutation {
      bulkOperationRunQuery(
       query: """
        {
          products {
            edges {
              node {
                id
                variants(first: 5)
            {
              edges {
                node {
                    id
                  sku
                  inventoryItem {
                    id  #this is the inventory_item_id
                  }
                }
              }
            }
              }
            }
          }
        }
        """
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }

    1. After completed I then grabbed the Url returned from the Bulk Operation:

    query {
      node(id: "gid://shopify/BulkOperation/123456789") {
        ... on BulkOperation {
          url
          partialDataUrl
        }
      }
    }

    1. When we go to the URL we get a downloadable .jsonl file. I parsed it with PHP to grab only the sku and InventoryItem id:

    print '<table>';
    $file = @fopen("bulk.json", "r");
    while (!feof($file)){
        $line = fgets($file);
        $obj = json_decode($line);
        $sku = $obj->sku;
        $inv_id = $obj->inventoryItem->id;
        $inv_id = str_replace("gid://shopify/InventoryItem/", "", $inv_id);
        if($inv_id != ''){
        print "<tr>
        <td>$sku</td>";
        print "<td>$inv_id</td>
        </tr>";
        }
    }
    print '</table>';

    This provided me with all 9,000 Products Inventory Item IDs to be used on any subsequent Inventory Updates we need to call with GraphQL.


  2. You can export inventory in bulk and get your needed inventory item IDs, current quantities and location. Using that file (JSON) you can then loop through those and activate your inventory items.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search