To create a product with as many properties and custom attributes as possible, you would need to include all the available fields in the CatalogObject instance. Here’s an example of how you might structure your payload:
const catalogObject = {
idempotencyKey: `${newProduct.sku}-${Date.now()}`,
object: {
id: `#${newProduct.sku}`, // Square ID should start with # for custom objects
type: 'ITEM',
itemData: {
name: newProduct.name,
description: newProduct.description,
abbreviation: slugify(newProduct.name),
categoryId: newProduct.categoryId,
taxIds: newProduct.taxIds,
modifierListIds: newProduct.modifierListIds,
variations: [
{
id: `#${newProduct.sku}-variation`,
type: 'ITEM_VARIATION',
itemVariationData: {
itemId: `#${newProduct.sku}`,
name: `${newProduct.name} Variation`,
pricingType: 'FIXED_PRICING',
priceMoney: {
amount: Math.round(newProduct.price.value * 100),
currency: 'USD'
},
sku: newProduct.sku,
trackInventory: true,
inventoryAlertType: 'LOW_QUANTITY',
inventoryAlertThreshold: 5,
userData: newProduct.userData,
serviceDuration: newProduct.serviceDuration,
itemOptionValues: newProduct.itemOptionValues
}
}
],
productType: 'REGULAR',
skipModifierScreen: false,
itemOptions: newProduct.itemOptions,
sortName: newProduct.sortName
}
}
};
In this example, I’ve added more fields to the itemData
and itemVariationData
objects. These include categoryId
, taxIds
, modifierListIds
, trackInventory
, userData
, serviceDuration
, itemOptionValues
, productType
, skipModifierScreen
, itemOptions
, and sortName
.
Please note that some of these fields like categoryId
, taxIds
, modifierListIds
, and itemOptionValues
require IDs of existing catalog objects. You would need to create these objects separately using the Catalog API before you can reference them in your product creation payload.
Also, remember that the customAttributeValues
field in your payload should be an object where the keys are the IDs of the custom attribute definitions and the values are CatalogCustomAttributeValue objects.
Please refer to the Square Catalog API documentation for more details on each of these fields and how to use them.
This answer was reviewed by @Bryan-Square.