Imagine, you have a shop that offers food products. As required in many countries you are required to provide the nutrition facts about your food products. To do so, you need to provide quantities for, e.g., sugar, fat, and protein for each of the products you offer. Look at the yummy tub of ice cream below for which we need to store some nutrition facts.
Wouldn’t It would be great to have a structured data object that holds all those nutrient information and that we could reuse in all our products? That’s really not a big deal in the commercetools platform, you can easily define ProductTypes with custom attributes the products of that type should have in common and you can nest this ProductType into another one. Confused? Don’t worry, we’ll guide you step by step through this tutorial.
Create simple Product Type
Our objective is to construct a food-product-type that contains nutrition information captured in a ProductTypenutrient-information. Moreover, the food-product-type does not only contain one object of the type nutrient-information, but a structure such as a Set of them, e.g., one for fat, one for sugar, and so on as depicted in the figure below:.
We always need to start with the creation of the simple ProductType that should be nested into the advanced ProductType. We must do it this way since we need the typeID of the ProductType to be nested first. Otherwise we wouldn’t be able to setup the reference relationship between the advanced ProductType and the nested ProductType properly.
For our example we need to design the nutrient-informationProductType first. To keep it simple this ProductType contains two attributes only: one that holds a number indicating the quantity of a nutrient (quantityContained) and one attribute for the type of nutrient (nutrientTypeCode). We’ll set both attributes as mandatory to be filled with values by setting isRequired to true for each attribute. For now nested attributes are not searchable in the commercetools platform so that we need to set is Searchable to false.
After we created the product type to be nested we can now create our advanced food-product-type by referencing the previously created nutrient-information product type in a nested attribute we call nutrients. Furthermore we add the attribute taste that gives a textual description about how excellent our food product tastes.
We now have everything we need for creating an example-food-product of our food-product-type. The taste of our example-food-product is excellent of course, but we are also nesting following nutrient-information into our example-food-product: 1.4 units of FAT and 1.15 units of SUGAR. For our example we have reused the nutrient-information product type two times, but you can add more if you need since we defined the nutrients attribute of our food-product-type as set of nutrient-information.
What have we just done? Basically, we have defined a ProductType that we reused in another ProductType. This allows us the composition of advanced product types based on existing ones. The commercetools platform supports that by using its nestedAttributeType as illustrated in the figure below:
The following JSON snippet shows how an existing ProductType is nested into an AttributeDefinition of another ProductType in the commercetools platform: