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
  • Implementing a Task
  • Adding a Task
  • Extension method example
  • Register your custom pipeline tasks, using an extension method

Was this helpful?

  1. Ucommerce Next Gen
  2. Extensions

Extending Pipelines

Modify system behavior by modifying Pipelines.

PreviousExtensionsNextOrder Processing Pipelines

Last updated 1 year ago

Was this helpful?

Ucommerce Pipelines are the primary extension points where you can execute any custom logic to fit your requirements. These extensions could be changing the existing behavior of the pipelines, adding new steps to be executed, and integrating with other systems.

Implementing a Task

Before implementing a new Pipeline Task, we must , specifically the input and output parameter types. In the examples below, we'll use XPipelineInput and XPipelineOutput to signify the types, as they follow the naming convention of the Pipeline name followed by Input or Output.

  • Create a new Class.

  • Inherit from IPipelineTask<XPipelineInput, XPipelineOutput>

  • Implement the Execute method.

Context.Input contains information passed to the Pipeline when it is being executed.

Context.Output contains information that will be returned once the Pipeline is finished executing.

A Pipeline will save changes in the current database context as the last task. Any tracked entity changes will be saved.

public class MyCustomTask : IPipelineTask<XPipelineInput, XPipelineOutput>
{
    public CascadeMode CascadeMode { get; } = CascadeMode.Continue;

    public Task Execute(PipelineContext<XPipelineInput, XPipelineOutput> context, CancellationToken cancellationToken)
    {
        // Your custom logic.
    }
}

Adding a Task

The next step is registering the PipelineTask in the IoC container for the proper Pipeline and position. This can be done via the Ucommerce builder, and it is recommended to use a helper method for reusability and clarity.

Extension method example

This example displays different ways of modifying a pipeline.

/// <summary>
/// Extension for adding my custom pipeline task.
/// </summary>
public static class CustomPipelineExtensions
{
    /// <summary>
    /// Adds Custom Cart Extension.
    /// </summary>
    public static IUcommerceBuilder AddMyCartExtension(this IUcommerceBuilder builder)
    {
        //These examples all modify the default 'CalculateCart' pipeline
        
        //This will insert the task 'MyCustomTask1' just before 'SaveUpdatedCartPipelineTask'
        builder.InsertPipelineTaskBefore<IPipelineTask<CalculateCartInput, CalculateCartOutput>, MyCustomTask1>(
            typeof(SaveUpdatedCartPipelineTask));
            
        //This will insert it just after 'SaveUpdatedCartPipelineTask'
        builder.InsertPipelineTaskAfter<IPipelineTask<CalculateCartInput, CalculateCartOutput>, MyCustomTask2>(
            typeof(SaveUpdatedCartPipelineTask));
            
        //This will insert it as the first task of the pipeline
        builder.InsertPipelineTaskFirst<IPipelineTask<CalculateCartInput, CalculateCartOutput>, MyCustomTask3>();
        
        //This will insert it as the last task of the pipeline
        builder.InsertPipelineTaskLast<IPipelineTask<CalculateCartInput, CalculateCartOutput>, MyCustomTask4>();
        
        //This will remove the default task 'SaveUpdatedCartPipelineTask' from the pipeline
        builder.RemovePipelineTask<IPipelineTask<CalculateCartInput, CalculateCartOutput>, SaveUpdatedCartPipelineTask>();
        
        return builder;
    }
}

Replace tasks by placing yours before or after the default task, then remove the default task.

Register your custom pipeline tasks, using an extension method

Now you can register all of your customized pipeline tasks using your extension method like this.

builder.Services.AddUcommerce(builder.Configuration)
    // ... register other Ucommerce components first
    .AddMyCartExtension()
    .Build();

When adding custom tasks, remember to do it after adding all of the Ucommerce components. This way, you can be sure that your custom logic is added in the desired order and registered correctly.

identify which pipeline we want to extend