I am using the below code to add items to a catalog. Each item has the same custom attribute"Brand"with different values obviously. I am getting this error
{
"errors": [
{
"category": "INVALID_REQUEST_ERROR",
"code": "BAD_REQUEST",
"detail": "Unknown custom attribute definition with id `#brand` referred to in object with id `#cat301315-BLK-L`"
}
]
}
$uniqid = uniqid();
$priceInCents = $product['price'] * 100;
// Create the Money object for the price
$price_money = new \Square\Models\Money();
$price_money->setAmount((int) $priceInCents); // Amount in cents, e.g., $10.00 should be 1000
$price_money->setCurrency('USD'); // Set the currency, e.g., USD
//Brand custom attribute
$allowed_object_types = ['ITEM', 'ITEM_VARIATION'];
$custom_attribute_definition_data = new \Square\Models\CatalogCustomAttributeDefinition('STRING', 'Brand', $allowed_object_types);
$custom_attribute_definition_data->setType('STRING');
$custom_attribute_definition_data->setKey('brand');
$custom_attribute_definition_data->setName('Brand');
$custom_attribute_definition_data->setAppVisibility('APP_VISIBILITY_READ_WRITE_VALUES');
$custom_attribute_definition_data->setSellerVisibility('SELLER_VISIBILITY_READ_WRITE_VALUES');
$custom_attribute_definition_data->setAllowedObjectTypes($allowed_object_types);
$catalog_object = new \Square\Models\CatalogObject('CUSTOM_ATTRIBUTE_DEFINITION', '#brand');
$catalog_object->setType('CUSTOM_ATTRIBUTE_DEFINITION');
$catalog_object->setCustomAttributeDefinitionData($custom_attribute_definition_data);
$catalog_custom_attribute_value = new \Square\Models\CatalogCustomAttributeValue();
$catalog_custom_attribute_value->setName('Brand');
$catalog_custom_attribute_value->setStringValue($product['brand']);
$catalog_custom_attribute_value->setCustomAttributeDefinitionId('#brand');
$catalog_custom_attribute_value->setType('STRING');
$catalog_custom_attribute_value->setKey('brand');
$custom_attribute_values = ['brand' => $catalog_custom_attribute_value];
// Create the CatalogItemVariation object
$item_variation_data = new \Square\Models\CatalogItemVariation();
$item_variation_data->setItemId('#cat' . $product['sku']); // Temporary ID for new items
$item_variation_data->setSku($product['sku']);
$item_variation_data->setName($product['name']);
$item_variation_data->setPricingType('FIXED_PRICING');
$item_variation_data->setPriceMoney($price_money);
$item_variation_data->setUpc($product['UPC']);
// Create the CatalogObject for the item variation
$catalog_object_variation = new \Square\Models\CatalogObject('ITEM_VARIATION', '#' . $product['sku']);
$catalog_object_variation->setItemVariationData($item_variation_data);
// Create the CatalogItem object
$item_data = new \Square\Models\CatalogItem();
$item_data->setName($product['name']);
$item_data->setDescription("description"); // Assuming you have a description field
$item_data->setAbbreviation($product['sku']);
$item_data->setVariations([$catalog_object_variation]);
$item_data->setTaxIds($this->getTaxIdString($product));
// Create the CatalogObject for the item
$catalog_object_item = new \Square\Models\CatalogObject('ITEM', '#cat' . $product['sku'],);
$catalog_object_item->setCustomAttributeValues($custom_attribute_values);
$catalog_object_item->setItemData($item_data);
$objects[] = $catalog_object_item;
This is the request taken from the dashboard
"batches": [
{
"objects": [
{
"type": "ITEM",
"id": "#cat301315-BLK-L",
"custom_attribute_values": {
"brand": {
"name": "Brand",
"string_value": "Salty Crew",
"custom_attribute_definition_id": "#brand",
"type": "STRING",
"key": "brand"
}
},
"item_data": {
"name": "75035012W Seafarer 1-Piece Blk-L",
"description": "description",
"abbreviation": "301315-BLK-L",
"tax_ids": [],
"variations": [
{
"type": "ITEM_VARIATION",
"id": "#301315-BLK-L",
"item_variation_data": {
"item_id": "#cat301315-BLK-L",
"name": "75035012W Seafarer 1-Piece Blk-L",
"sku": "301315-BLK-L",
"upc": "804046559023",
"pricing_type": "FIXED_PRICING",
"price_money": {
"amount": 8995,
"currency": "USD"
}
}
}
]
}
},
I have been trying to sort this out for hours. Please help!
THanks
Don