- Subscribe to RSS Feed
- Mark Thread as New
- Mark Thread as Read
- Float this Thread for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
Creating an item in the catalog via the API (PHP)
Hello,
I'm trying to create items thru the API using the provided PHP SDK, however I'm coming across an issue. When I follow along with the setup guide with its example (https://docs.connect.squareup.com/products/catalog/setup), I get the following error.
Exception when calling CatalogApi->batchUpsertCatalogObjects: [HTTP/1.1 400 Bad Request] {"errors":[{"category":"INVALID_REQUEST_ERROR","code":"VALUE_EMPTY","detail":"Field must not be blank","field":"batches[0].objects[0].item_data.variations[0].id"}]}
Any help would be greatly appreciated.
Here is the object I am sending
<pre>SquareConnect\Model\CatalogObjectBatch Object
(
[objects:protected] => Array
(
[0] => SquareConnect\Model\CatalogObject Object
(
[type:protected] => ITEM
[id:protected] => #itemID
[updated_at:protected] =>
[version:protected] =>
[is_deleted:protected] =>
[catalog_v1_ids:protected] =>
[present_at_all_locations:protected] =>
[present_at_location_ids:protected] =>
[absent_at_location_ids:protected] =>
[item_data:protected] => SquareConnect\Model\CatalogItem Object
(
[name:protected] => Nintendo Switch - Neon Blue and Red Joy-Con
[description:protected] => 045496590093
[abbreviation:protected] =>
[label_color:protected] =>
[available_online:protected] =>
[available_for_pickup:protected] =>
[available_electronically:protected] =>
[category_id:protected] =>
[tax_ids:protected] =>
[modifier_list_info:protected] =>
[image_url:protected] =>
[variations:protected] => Array
(
[0] => SquareConnect\Model\CatalogObject Object
(
[type:protected] => ITEM_VARIATION
[id:protected] => #itemID
[updated_at:protected] =>
[version:protected] =>
[is_deleted:protected] =>
[catalog_v1_ids:protected] =>
[present_at_all_locations:protected] =>
[present_at_location_ids:protected] =>
[absent_at_location_ids:protected] =>
[item_data:protected] => SquareConnect\Model\CatalogItemVariation Object
(
[item_id:protected] => #itemID
[name:protected] => Nintendo Switch - Neon Blue and Red Joy-Con
[sku:protected] =>
[upc:protected] =>
[ordinal:protected] =>
[pricing_type:protected] =>
[price_money:protected] => SquareConnect\Model\Money Object
(
[amount:protected] => 19435
[currency:protected] => USD
)
[location_overrides:protected] =>
[track_inventory:protected] =>
[inventory_alert_type:protected] =>
[inventory_alert_threshold:protected] =>
[user_data:protected] =>
[service_duration:protected] =>
)
[category_data:protected] =>
[item_variation_data:protected] =>
[tax_data:protected] =>
[discount_data:protected] =>
[modifier_list_data:protected] =>
[modifier_data:protected] =>
)
)
[product_type:protected] =>
[skip_modifier_screen:protected] =>
)
[category_data:protected] =>
[item_variation_data:protected] =>
[tax_data:protected] =>
[discount_data:protected] =>
[modifier_list_data:protected] =>
[modifier_data:protected] =>
)
)
)
</pre>
And here is the code I'm using
$defaultApiConfig = new \SquareConnect\Configuration();
$defaultApiConfig->setAccessToken($this->accessToken);
$defaultApiClient = new \SquareConnect\ApiClient($defaultApiConfig);
$catalogApi = new SquareConnect\Api\CatalogApi($defaultApiClient);
$price = new \SquareConnect\Model\Money;
$price->setAmount($data["price"] * 100);
$price->setCurrency('USD');
$itemData = new \SquareConnect\Model\CatalogItem;
$itemData->setName($data["name"]);
$itemData->setDescription($data["upc"]);
$item = new \SquareConnect\Model\CatalogObject;
$item->setType("ITEM");
$item->setId("#itemID");
$varData = new \SquareConnect\Model\CatalogItemVariation;
$varData->setName($data["name"]);
$varData->setItemId("#itemID");
$varData->setPriceMoney($price);
$varData->setUpc($data["upc"]);
$varData->setSku($data["sku"]);
$var = new \SquareConnect\Model\CatalogObject;
$var->setType("ITEM_VARIATION");
$var->setItemData($varData);
$itemData->setVariations(array($var));
$item->setItemData($itemData);
$batch = new \SquareConnect\Model\CatalogObjectBatch;
$batch->setObjects(array($item));
$body = new \SquareConnect\Model\BatchUpsertCatalogObjectsRequest();
$body->setIdempotencyKey(uniqid()); // uniqid() generates a random string.
$batches = array($batch);
$body->setBatches($batches);
try {
$result = $catalogApi->batchUpsertCatalogObjects($body);
echo 'Success!';
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling CatalogApi->batchUpsertCatalogObjects: ', $e->getMessage(), PHP_EOL;
}
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report

- Subscribe to RSS Feed
- Mark Thread as New
- Mark Thread as Read
- Float this Thread for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
Hey @Jonathon!
It looks like you're passing in item_data on the variation object. When it should be item_variation_data. Also you will need a separate id associated to the item_variation _id that is the same format as the item_id, but not identical.
If you still need help please feel free to reach out to me.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report