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));
TA贡献1816条经验 获得超6个赞
object_id
s 与 SKU 不同;它们是 Square 这边生成的唯一 ID。您可能希望使用SearchCatalogObjects (POST /v2/catalog/search) 端点来按 SKU 进行搜索。使用您的 SKU 之一的查询示例如下:
{
"query": {
"exact_query": {
"attribute_name": "sku",
"attribute_value": "GFLR20L"
}
}
}
这将获取您的目录对象 ID,但如果您对库存感兴趣,您仍然需要使用另一个端点来获取库存,例如RetrieveInventoryCount(它以catalog_object_id
s 作为参数)。
- 2 回答
- 0 关注
- 125 浏览
添加回答
举报