为了账号安全,请及时绑定邮箱和手机立即绑定

Laravel + Shopify inventoryBulkAdjustQuantity

Laravel + Shopify inventoryBulkAdjustQuantity

PHP
一只名叫tom的猫 2023-05-12 10:35:05
我正在使用以下包:'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        ],    );但我不明白如何使用它。谁能帮帮我?
查看完整描述

3 回答

?
心有法竹

TA贡献1866条经验 获得超5个赞

现在可以了。如果您以前从未使用过 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    

    );


查看完整回答
反对 回复 2023-05-12
?
慕姐4208626

TA贡献1852条经验 获得超7个赞

不太好的例子(硬编码值,别名 - 不是现实生活中的例子)......应该使用 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

 );


查看完整回答
反对 回复 2023-05-12
?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

我想展示另一个使用它的库并扩展最后一个例子。


我为 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)。


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

删除产品有效(如果库初始化良好,这是一个很好的测试):


$query = <<<QUERY

  mutation {

    productDelete(input: {id: "gid://shopify/Product/6975310463182"})

    {

      deletedProductId

    }

  }

QUERY;

$response = $client->query(["query" => $query]);


print_r($response);

die;


查看完整回答
反对 回复 2023-05-12
  • 3 回答
  • 0 关注
  • 122 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信