skip to Main Content

I’m using following package : ‘osiset/Basic-Shopify-API’ and need bulk update products by location.
It’s only possible with GraphQL. This function should work :
inventoryBulkAdjustQuantityAtLocation Shopify documentation

    $shop = 'example.myshopify.com';
    $token = 'shppa_admin_api_token';

    / Create options for the API
    $options = new Options();
    $options->setVersion('2020-04');

     // Create the client and session
    $api = new BasicShopifyAPI($options);
    $api->setSession(new Session($shop, $token));


     $products[0]['inventoryItemId'] = '33125243617303';
    $products[0]['availableDelta'] = 2000;

    $result = $api->graph(
        'mutation inventoryBulkAdjustQuantityAtLocation($inventoryItemAdjustments: InventoryAdjustItemInput!,$locationId: ID!) 
                 {inventoryBulkAdjustQuantityAtLocation(inventoryItemAdjustments: $InventoryAdjustItemInput, locationId: $locationId) {userErrors {field message } inventoryLevels { id }}}',
        ['inventoryItemAdjustments' => 

            $products

        ],

    );

But I don’t understand how to use it. Could anyone help me ?

3

Answers


  1. Chosen as BEST ANSWER

    Now it works. It's a challenge to understand GraphQL queries if you never used them before.

    Here are some more information :

    https://www.shopify.com/partners/blog/multi-location_and_graphql

     $locationId = "gid://shopify/Location/1";
    
        $inventoryItemAdjustments1['locationId'] = $locationId;
        $inventoryItemAdjustments1['inventoryItemAdjustments']['inventoryItemId'] = 'gid://shopify/InventoryItem/1';
        $inventoryItemAdjustments1['inventoryItemAdjustments']['availableDelta'] = 500;
    
        $result = $api->graph('mutation inventoryBulkAdjustQuantityAtLocation($inventoryItemAdjustments: [InventoryAdjustItemInput!]!, $locationId: ID!) 
        {inventoryBulkAdjustQuantityAtLocation(inventoryItemAdjustments: $inventoryItemAdjustments, locationId: $locationId) {userErrors { field message }}}',
        $inventoryItemAdjustments1    
        );
    

  2. Not so good examples (hardcoded values, aliases – not real life examples) … graphql variables should be used and they should match mutation requirements (‘root’ parameters), in this case locationId and inventoryItemAdjustments (array of objects).

    You can test this mutation in graphiql/playground using ‘query variables’ defined like this:

    {
      locationId: "gid://shopify/Location/1",
      inventoryItemAdjustments: [
        { 
          'inventoryItemId': 'gid://shopify/InventoryItem/1',
          'availableDelta': 500
        },
        { 
          'inventoryItemId': 'gid://shopify/InventoryItem/2',
          'availableDelta': 100
        }
      ]
    }
    

    … so using php (associative arrays are encoded to json as objects – explicitely declared for readability) it should look more like this:

     $locationId = "gid://shopify/Location/1";
     $inventoryItemAdjustments = [];
     $inventoryItemAdjustments[] = (object)[
       'inventoryItemId' => 'gid://shopify/InventoryItem/1',
       'availableDelta'] => 500;
     ];
     $inventoryItemAdjustments[] = (object)[
       'inventoryItemId' => 'gid://shopify/InventoryItem/2',
       'availableDelta'] => 100;
     ];
    
     $variables = (object)[
       'locationId' => $locationId;
       'inventoryItemAdjustments' => $inventoryItemAdjustments
     ];
    
     $result = $api->graph('mutation inventoryBulkAdjustQuantityAtLocation($inventoryItemAdjustments: [InventoryAdjustItemInput!]!, $locationId: ID!) 
        {inventoryBulkAdjustQuantityAtLocation(inventoryItemAdjustments: $inventoryItemAdjustments, locationId: $locationId) {userErrors { field message }}}',
        $variables
     );
    
    Login or Signup to reply.
  3. I would like to show another library that uses this and expand on the last example.

    I am using a slightly different library for graphql:
    https://github.com/Shopify/shopify-php-api/

    Updating the inventory like it was posted here shows a [statusCode:GuzzleHttpPsr7Response:private] => 200
    So it seems to work but does not reflect in updated inventory. 🙁

    Checking at /admin/products/inventory?location_id=62241177806&query=F11_27781195
    would not show the new inventory.
    I am using the inventoryid correctly (not product or variantid).

     $inventoryItemAdjustments = array();
     
     $inventoryItemAdjustments[] = (object)[
       'inventoryItemId' => 'gid://shopify/InventoryItem/43151435235534',
       'availableDelta' => 500
     ];
     $inventoryItemAdjustments[] = (object)[
       'inventoryItemId' => 'gid://shopify/InventoryItem/43151435268302',
       'availableDelta' => 500
     ];
    
     $variables = array(
       'locationId' => ConfigClass::$locationId,
       'inventoryItemAdjustments' => $inventoryItemAdjustments
     );
    
    $graphqlquery='mutation inventoryBulkAdjustQuantityAtLocation($inventoryItemAdjustments: [InventoryAdjustItemInput!]!, $locationId: ID!) 
        {inventoryBulkAdjustQuantityAtLocation(inventoryItemAdjustments: $inventoryItemAdjustments, locationId: $locationId) {userErrors { field message }}}';
    
    $response = $client->query(['query' => $graphqlquery, 'variables' => $variables]); 
    

    Deleting a product works (and is a good test if the library is initialized well):

    $query = <<<QUERY
      mutation {
        productDelete(input: {id: "gid://shopify/Product/6975310463182"})
        {
          deletedProductId
        }
      }
    QUERY;
    $response = $client->query(["query" => $query]);
    
    print_r($response);
    die;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search