Hi, I am totally a novice with this API. What I’m trying to do is query into my clients catalog and retrieve catalog items that are in a certain category and have an inventory quantity greater than 0.
I have been able to accomplish the former using something similar to this:
<?php
namespace Example;
use Square\SquareClient;
use Square\Environments;
use Square\Catalog\Requests\SearchCatalogObjectsRequest;
use Square\Types\CatalogObjectType;
use Square\Types\CatalogQuery;
use Square\Types\CatalogQueryExact;
$client = new SquareClient(
token: 'TOKEN',
options: [
'baseUrl' => Environments::Sandbox->value,
],
);
$client->catalog->search(
new SearchCatalogObjectsRequest([
'includeDeletedObjects' => false,
'objectTypes' => [
CatalogObjectType::Category->value,
CatalogObjectType::Item->value,
CatalogObjectType::Image->value,
],
'limit' => 1000,
'query' => new CatalogQuery([
'exactQuery' => new CatalogQueryExact([
'attributeName' => 'categories',
'attributeValue' => 'MFVBABN3AHQWTUO44KDUHJ4Z',
]),
]),
]),
);
However, it doesn’t seem that I can get inventory quantities from the same query.
I can take the returned IDs and do a batchGetCounts (below) to get their quantities, then only show the items with inventory.
`$client->inventory->batchGetCounts(`
` new BatchGetInventoryCountsRequest([`
` 'catalogObjectIds' => [`
` 'returned_ids',`
` ]`
` ]),`
`);`
But is there a better way where I’m only running one query?
Thanks.
I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:
Additional Documentation
Inventory API
Search for Items and Objects
Catalog API
If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.
To achieve your goal of retrieving catalog items in a certain category with an inventory quantity greater than 0, you will need to perform two separate queries, as the Catalog API and Inventory API handle different aspects of the data.
-
Search for Catalog Items by Category: Use the SearchCatalogObjects endpoint to find items in the specified category. This will give you the catalog object IDs of the items.
-
Retrieve Inventory Counts: Use the BatchRetrieveInventoryCounts endpoint to get the inventory counts for the catalog object IDs you obtained from the first query. This will allow you to filter out items with an inventory quantity greater than 0.
Unfortunately, there isn’t a single query that combines both catalog and inventory data directly. The separation of concerns between the Catalog API and Inventory API means you need to first identify the items and then check their inventory status. This two-step process is necessary to achieve the desired result.
This answer was reviewed by @Bryan-Square.