Insights, perspectives, and stories on software, business, and innovation.
This post provides a technical walkthrough, using PHP in a procedural function style, to demonstrate how to use the modern inventorySetQuantities GraphQL mutation to set absolute inventory levels.
Why inventorySetQuantities?
The deprecated inventoryActivate mutation was primarily used to enable inventory tracking at a location, optionally setting an initial available quantity. The newer mutations provide a clearer separation of concerns and better auditability:
inventorySetQuantities: Used to set the absolute quantity for an inventory item at one or more locations. This is the recommended mutation for systems acting as the source of truth for inventory.
inventoryAdjustQuantities: Used to apply a delta (a positive or negative change) to the existing quantity.
Using inventorySetQuantities ensures your inventory system remains up-to-date with Shopify's best practices, allowing you to set the exact quantity you want to be recorded.
I'm going to presume for now you have the Shopify GraphQL delivery methods already to go in your application (using cURL and pre-saved Shopify credentials and access tokens etc).
The most common usage case here is to update the absolute stock levels for all the variations in a given product... this is especially the case in a lot of our fashion-oriented clients.
In all of our middleware applications with clients, we always store an up-to-date master list of products and variations (I'll detail how we do this in a future post). This is essential to give us the information we need to leverage the Shopify mutation and do all the variations in one go.
/**
* Executes a GraphQL mutation to set the inventory quantities for some/all of the variations for a Shopify product .
*
* @param string $inventory_items An array of the variations you want to update on this call.
* Each variation structured as its own associative array as follows;
* [
* "inventoryItemId" => "xxx", // either bigint/int64 numeric or in Shopify gid form; gid://Shopify/InventoryItem/xxxxxx
* "locationId" => "xxx", // either bigint/int64 numeric or in Shopify gid form; gid://Shopify/Location/xxxxxx
* "quantity" => 999 //this is the new absolute quantity to set for this variation
* ]
* @param string $reason A descriptive reason for the inventory change. Defaults to "correction" as this is the preferred value for an absolute update.
* @return array|bool The decoded response array on success, or false on cURL error.
*/
function update_shopify_inventory(
$inventory_items = [],
$reason = 'correction'
) {
###### 1. Define the GraphQL Mutation #######
$query = '
mutation InventorySet($input: InventorySetQuantitiesInput!) {
inventorySetQuantities(input: $input) {
inventoryAdjustmentGroup {
createdAt
reason
}
userErrors {
field
message
}
}
}
';
###### 2. Define the Mutation Variables ######
//first setup the GraphQL variables structure
$variables = [
'input' => [
'reason' => $reason,
'quantities' => []
]
];
//now loop round the incoming inventory items and update $variables
//we use the basename native function and then prepend of a gid to ensure the incoming values result in a gid format;
foreach($inventory_items as $inventory_item){
$variables['input']['quantities][]=[
'inventoryItemId' => 'gid://Shopify/InventoryItem/' . basename($inventory_item['inventoryItemId']),
'locationId' => 'gid://Shopify/Location/' . basename($inventory_item['locationId']),
'quantity' => (int)$inventory_item['quantity']
];
}
###### 3. Construct the API Request Payload ######
$payload = json_encode([
'query' => $query,
'variables' => $variables
]);
###### 4. Post payload to Shopify ######
//{....}
}
By migrating from the deprecated inventoryActivate to the powerful inventorySetQuantities mutation, you ensure your integration is robust, compliant with current API standards, and offers a clear audit trail for your inventory updates.
Dev Partners Ltd can help you leverage the GraphQL API to build robust, scalable integrations for your Shopify, PIM (Product Information Management), and ERP (Enterprise Resource Planning) projects. Master your data flow and streamline your operations. Contact us now!
Published“After ongoing issues and delays with our previous developers, we brought in Rob & Jason at Dev Partners to take over all our websites and bespoke software. What a breath of fresh air - they were fully focused, with excellent pre-launch designs and clear communication throughout. Highly recommended to anyone.”
If clunky systems are slowing you down, let’s talk. No sales patter. No corporate waffle. Just a straightforward conversation about fixing the stuff that wastes your time.
Two normal blokes who happen to be good at untangling messy systems. We’ll talk to you like humans, not consultants, and help you find the simplest way forward.
We’ll reply within one working day. No spam. No waffle.