Hello,
Is it possible to assign values to catalog custom attributes that were created via the dashboard with the API?
For example we have a custom attribute “Brand” and I would like to set the value to whatever the items brand is when upserted via the api.
Thanks
Don
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
Custom Attributes
Manage Customer Custom Attributes
Custom Attributes for Customers
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.
Yes, it is possible to assign values to catalog custom attributes that were created via the dashboard using the API. You can use the UpsertCatalogObject endpoint to create a CatalogCustomAttributeDefinition object by specifying CUSTOM_ATTRIBUTE_DEFINITION as the CatalogObject type and assigning the appropriate data to the custom_attribute_definition_data object. This includes the type of custom attribute, allowed hosting object types, and the visibility.
Once the custom attribute is defined, you can assign a value to the custom attribute on a specified hosting catalog item, item variation, or modifier.
Remember, the key of the custom attribute in the API should match the key of the custom attribute created in the dashboard. Also, the visibility of the custom attribute must be set to VISIBILITY_READ_WRITE_VALUES to be able to write values to it via the API.
This answer was reviewed by @Bryan-Square.
Thanks for the reply.
I am getting an error
Error upserting item: Array ( [0] => Square\Models\Error Object ( [category:Square\Models\Error:private] => INVALID_REQUEST_ERROR [code:Square\Models\Error:private] => NOT_FOUND [detail:Square\Models\Error:private] => The provided Custom Attribute Key: Brand was not found [field:Square\Models\Error:private] => ) )
When using the below code. The name of the custom attribute in the dashboard is Brand. Is that the Key for custom attribute in the API?
I am doing this to set it up
//Brand custom attribute
$catalog_custom_attribute_value = new \Square\Models\CatalogCustomAttributeValue();
$catalog_custom_attribute_value->setName('Brand');
$catalog_custom_attribute_value->setStringValue($product['brand']);
$custom_attribute_values = ['Brand' => $catalog_custom_attribute_value];
$allowed_object_types = ['ITEM', 'ITEM_VARIATION'];
$string_config = new \Square\Models\CatalogCustomAttributeDefinitionStringConfig();
$string_config->setEnforceUniqueness(false);
$custom_attribute_definition_data = new \Square\Models\CatalogCustomAttributeDefinition('STRING', 'Brand', $allowed_object_types);
$custom_attribute_definition_data->setType('STRING');
$custom_attribute_definition_data->setDescription('The products brand');
$custom_attribute_definition_data->setSellerVisibility('SELLER_VISIBILITY_READ_WRITE_VALUES');
$custom_attribute_definition_data->setAppVisibility('APP_VISIBILITY_READ_WRITE_VALUES');
$custom_attribute_definition_data->setStringConfig($string_config);
$custom_attribute_definition_data->setKey('Brand');
Then added this to the catalog object
$catalog_object_item->setCustomAttributeValues($custom_attribute_values);
$catalog_object_item->setCustomAttributeDefinitionData($custom_attribute_definition_data);
Also in the api explorer
The new \Square\Models\CatalogCustomAttributeDefinition( ‘Brand’, $allowed_object_types);
line only has two arguments after getting an error I searched the documentation for what the third argument should be and eventually found it in the source code on GitHub.
Glad you were able to figure this out. Thanks for sharing all your findings.