Ucommerce
  • Ucommerce Next Gen
    • Getting Started
      • Prerequisites
      • Licensing
      • Ucommerce Templates
      • Headless Template
      • MVC Template
    • Headless
      • Postman Collection
      • Headless API Authentication
        • Token endpoint - Authorization Header
        • Authorization Scopes
        • Refreshing the Access Token
      • Reference
        • Cart
        • Cart / Order Line Items
        • Shipment
        • Billing
        • Promotion Codes
        • Price Groups
        • Payment Methods
        • Countries
        • Shipping Methods
        • Catalogs
        • Cart Custom Properties
        • Line Item Custom Properties
        • Orders
        • Views for Cart modifying operations
      • Custom Headless APIs
      • Error Handling
      • Pagination
      • Deprecation
    • Backoffice Authentication
      • Microsoft Entra ID Example
      • Auth0 Authentication Example
    • Definitions
      • What is a Definition
    • Search and indexing
      • Configuration
      • Indexing
        • Index Definitions
        • Facets
        • Indexing Prices
        • Suggestions
        • Custom Data
      • Searching
    • Payment Providers
      • Stripe Provider Integration
      • Implementing a custom payment provider
    • Data Import
    • Miscellaneous
      • Media
      • Price Group Inheritance
      • Price Group Criteria
      • Soft Deletion Of Entities
      • Logging
      • OpenTelemetry
    • Extensions
      • Extending Pipelines
        • Order Processing Pipelines
        • Checkout Pipelines
      • Changing Service Behavior
        • Images
        • Content
      • Custom Headless APIs
      • Extend the Backoffice
        • Custom UI Components
      • Custom Editor UI
      • Custom Promotion Criteria
      • Custom Price Group Criteria
    • How-To
      • Migrate from Classic
        • Common database issues
      • Entities from code
        • Bootstrapping data on startup
        • Product Definitions & Fields
      • Discover pipelines and their tasks
      • Executing a pipeline
    • Integrations
      • Umbraco Media Delivery API
      • App Slices
        • Product Picker
  • Release Notes
  • Contact Us
Powered by GitBook
On this page
  • Product Definition Fields
  • Product Definition
  • Create & Save
  • Related Articles

Was this helpful?

  1. Ucommerce Next Gen
  2. How-To
  3. Entities from code

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.

private ProductDefinitionFieldEntity CreateProductDefinitionField(
    DataTypeEntity dataType,
    string name,
    bool isMultilingual,
    bool isVariantProperty)
{
    return new ProductDefinitionFieldEntity
    {
        Name = name,
        Deleted = false,
        Multilingual = isMultilingual,
        DisplayOnSite = true,
        RenderInEditor = true,
        IsVariantProperty = isVariantProperty,
        DataType = dataType
    };
}

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.

private ProductDefinitionEntity CreateProductDefinition(
string name, 
string description)
{
    return new ProductDefinitionEntity
    {
        Name = name,
        Description = description,
        Deleted = false
    };
}
  • 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.

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        await using var asyncScope = _serviceProvider.CreateAsyncScope();
        var dbContext = asyncScope.ServiceProvider.GetRequiredService<UcommerceDbContext>();
        
        var exists = dbContext.Set<ProductDefinitionEntity>()
            .Any(x => x.Name == "Shirt");
        
        if (exists)
        {
            return;
        }
        
        var shortTextDataType = dbContext
            .Set<DataTypeEntity>()
            .First(x => x.DefinitionName == "ShortText");
        
        var shirtDefinition = CreateProductDefinition("Shirt", "Definition for Shirt type products.");
        
        shirtDefinition.ProductDefinitionFields = new List<ProductDefinitionFieldEntity>()
        {
            CreateProductDefinitionField(shortTextDataType, "Color", true, true),
            CreateProductDefinitionField(shortTextDataType, "Code", false, false)
        };
        
        dbContext.Add(shirtDefinition);
        
        await dbContext.SaveChangesAsync(stoppingToken);
    }

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

PreviousBootstrapping data on startupNextDiscover pipelines and their tasks

Last updated 11 months ago

Was this helpful?

Bootstrapping data on startup