Laravel + Shopify inventoryBulkAdjustQuantity

我正在使用以下包:'osiset/Basic-Shopify-API' 并且需要按位置批量更新产品。只有使用 GraphQL 才有可能。此功能应该有效:inventoryBulkAdjustQuantityAtLocation Shopify 文档

 $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


        ],


    );

但我不明白如何使用它。谁能帮帮我?


一只名叫tom的猫
浏览 125回答 3
3回答

心有法竹

现在可以了。如果您以前从未使用过 GraphQL 查询,那么理解它们是一个挑战。以下是更多信息: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        );

慕姐4208626

不太好的例子(硬编码值,别名 - 不是现实生活中的例子)......应该使用 graphql 变量,它们应该匹配突变要求('root'参数),在这种情况下和(对象数组locationId)inventoryItemAdjustments。您可以使用如下定义的“查询变量”在 graphiql/playground 中测试此突变:{  locationId: "gid://shopify/Location/1",  inventoryItemAdjustments: [    {       'inventoryItemId': 'gid://shopify/InventoryItem/1',      'availableDelta': 500    },    {       'inventoryItemId': 'gid://shopify/InventoryItem/2',      'availableDelta': 100    }  ]}...所以使用 php(关联数组被编码为 json 作为对象 - 为了可读性而明确声明)它应该看起来更像这样: $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 );

凤凰求蛊

我想展示另一个使用它的库并扩展最后一个例子。我为 graphql 使用了一个稍微不同的库: https://github.com/Shopify/shopify-php-api/像这里发布的那样更新库存显示 [statusCode:GuzzleHttp\Psr7\Response:private] => 200 所以它似乎有效但没有反映在更新的库存中。:(检查 /admin/products/inventory?location_id=62241177806&query=F11_27781195 不会显示新库存。我正确使用了 inventoryid(不是产品或 variantid)。&nbsp;$inventoryItemAdjustments = array();&nbsp;&nbsp;$inventoryItemAdjustments[] = (object)[&nbsp; &nbsp;'inventoryItemId' => 'gid://shopify/InventoryItem/43151435235534',&nbsp; &nbsp;'availableDelta' => 500&nbsp;];&nbsp;$inventoryItemAdjustments[] = (object)[&nbsp; &nbsp;'inventoryItemId' => 'gid://shopify/InventoryItem/43151435268302',&nbsp; &nbsp;'availableDelta' => 500&nbsp;];&nbsp;$variables = array(&nbsp; &nbsp;'locationId' => ConfigClass::$locationId,&nbsp; &nbsp;'inventoryItemAdjustments' => $inventoryItemAdjustments&nbsp;);$graphqlquery='mutation inventoryBulkAdjustQuantityAtLocation($inventoryItemAdjustments: [InventoryAdjustItemInput!]!, $locationId: ID!)&nbsp;&nbsp; &nbsp; {inventoryBulkAdjustQuantityAtLocation(inventoryItemAdjustments: $inventoryItemAdjustments, locationId: $locationId) {userErrors { field message }}}';$response = $client->query(['query' => $graphqlquery, 'variables' => $variables]);&nbsp;删除产品有效(如果库初始化良好,这是一个很好的测试):$query = <<<QUERY&nbsp; mutation {&nbsp; &nbsp; productDelete(input: {id: "gid://shopify/Product/6975310463182"})&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; deletedProductId&nbsp; &nbsp; }&nbsp; }QUERY;$response = $client->query(["query" => $query]);print_r($response);die;
打开App,查看更多内容
随时随地看视频慕课网APP