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

如何使用 PHP 按 SKU 获取方形库存?

如何使用 PHP 按 SKU 获取方形库存?

PHP
莫回无 2023-07-21 18:30:25
我想从 Square Catalog 获取所有产品。这是代码:require 'vendor/autoload.php';use Square\SquareClient;use Square\LocationsApi;use Square\Exceptions\ApiException;use Square\Http\ApiResponse;use Square\Models\ListLocationsResponse;use Square\Environment;$client = new SquareClient([    'accessToken' => '{{access_token}}',    'environment' => Environment::SANDBOX,]);//Providing SKU$object_ids = ['GFLR20L', '232GGGD'];$body = new \Square\Models\BatchRetrieveCatalogObjectsRequest($object_ids);$body->setIncludeRelatedObjects(true);$api_response = $client->getCatalogApi()->batchRetrieveCatalogObjects($body);if ($api_response->isSuccess()) {    $result = $api_response->getResult();} else {    $errors = $api_response->getErrors();}输出:object(Square\Models\BatchRetrieveCatalogObjectsResponse)#13 (3) {  ["errors":"Square\Models\BatchRetrieveCatalogObjectsResponse":private] => NULL  ["objects":"Square\Models\BatchRetrieveCatalogObjectsResponse":private] => NULL  ["relatedObjects":"Square\Models\BatchRetrieveCatalogObjectsResponse":private] => NULL }**> sjosey 的帖子建议:我的 PHP 代码:正在寻找名称为 Paper 的产品。**$object_types = ['ITEM'];$prefix_query = new \Square\Models\CatalogQueryPrefix('name', 'paper');$query = new \Square\Models\CatalogQuery();$query->setPrefixQuery($prefix_query);> 在此存储值$body = new \Square\Models\SearchCatalogObjectsRequest();$body->setObjectTypes($object_types);$body->setQuery($query);$body->setLimit(100);$api_response = $client->getCatalogApi()->searchCatalogObjects($body);> 在这里获取 api 响应if ($api_response->isSuccess()) {    $result = $api_response->getResult();} else {    $errors = $api_response->getErrors();}> 回波结果var_dump($result);
查看完整描述

2 回答

?
长风秋雁

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

想出了解决办法。以下代码按产品 IDS 获取所有产品的列表。该数组可用于根据要求设置数据(按 SKU 或任何内容)


require 'vendor/autoload.php';


use Square\SquareClient;

use Square\LocationsApi;

use Square\Exceptions\ApiException;

use Square\Http\ApiResponse;

use Square\Models\ListLocationsResponse;

use Square\Environment;


$client = new SquareClient([

    'accessToken' => '{{access_token}}',

    'environment' => Environment::PRODUCTION,

]);



$bag = [];

$cursor = null;

$ctr = 1;


$api_response = $client->getCatalogApi()->listCatalog($cursor, 'ITEM');


if ($api_response->isSuccess()) {

    $result = $api_response->getResult();

} else {

    $errors = $api_response->getErrors();

}


$g1 = $result;

$g2 = json_encode($g1);

$g3 = json_decode($g2);

$cursor = $g3->cursor;

$objects = $g3->objects;

$g4 = json_encode($objects);

$g5 = json_decode($g4);


foreach($g5 as $g51){

    $bag[$g51->id] = $g51;

}






while($cursor != null){


$api_response2 = $client->getCatalogApi()->listCatalog($cursor, 'ITEM');


if ($api_response2->isSuccess()) {

    $result2 = $api_response2->getResult();

} else {

    $errors2 = $api_response2->getErrors();

}


$g6 = $result2;

$g7 = json_encode($g6);

$g8 = json_decode($g7);

$cursor = $g8->cursor;

$objects2 = $g8->objects;

$g9 = json_encode($objects2);

$g10 = json_decode($g9);


foreach($g10 as $g101){

    $bag[$g101->id] = $g101;

}


}




var_dump(count($bag));


查看完整回答
反对 回复 2023-07-21
?
潇湘沐

TA贡献1816条经验 获得超6个赞

object_ids 与 SKU 不同;它们是 Square 这边生成的唯一 ID。您可能希望使用SearchCatalogObjects (POST /v2/catalog/search) 端点来按 SKU 进行搜索。使用您的 SKU 之一的查询示例如下:


{

  "query": {

    "exact_query": {

      "attribute_name": "sku",

      "attribute_value": "GFLR20L"

    }

  }

}

这将获取您的目录对象 ID,但如果您对库存感兴趣,您仍然需要使用另一个端点来获取库存,例如RetrieveInventoryCount(它以catalog_object_ids 作为参数)。



查看完整回答
反对 回复 2023-07-21
  • 2 回答
  • 0 关注
  • 125 浏览

添加回答

举报

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