Product Definitions & Fields
Creating Product Definitions and their related definition fields is the most frequent use for definitions. The taxonomy of products differs from implementation to implementation, and below are a few examples of how they can be created via code.
The code in these examples can and should be customized for individual use cases.
Product Definition Fields
Start by creating as many Product Definition Fields as needed for your Product Definition. We start with the fields because when creating the definition, we can then immediately add the fields before we save anything to the database.
The method above can be re-used to create multiple Product Definition Fields by supplying:
dataType - denoting the field should be displayed as, for example, a short text or a number field.
name - the name of the field that will appear on the product editor.
isMultilingual - if true, the field will be able to have differentiated values for each configured language.
isVariantProperty - if true, the field will be available only on variants; otherwise, it will be available only on products.
Product Definition
Continue with creating a Product Definition.
name - Name of the definition, denoting the type of product.
description - A description of what the definition is for.
Create & Save
The below example ties it all together. This is done within the context of a HostedService, which is covered in Bootstrapping data on startup.
Note the early return if the Definition already exists. Since this code runs on each startup, there must be a check so it only gets executed when necessary. In this case, if the definition already exists.
Create a scope within which to resolve a UcommerceDbContext.
Implement a check to decide if the bootstrapping should run on the database.
Find the right data type (in this example, we use ShortText for both fields).
Create the Product Definition using our helper method.
Create and add Product Definition Fields to the definition (in this example, we create two, one multilingual variant property for color and one non-multilingual product property for barcode)
Add the definition to the context and save changes to the database.
Once the application starts, the new definition will be selectable for products in the backoffice.
Related Articles
Bootstrapping data on startupLast updated