Umbraco Commerce
CMSCloudHeartcoreDXP
15.latest
15.latest
  • Umbraco Commerce Documentation
  • Release Notes
    • v15.1.0-Rc
    • v15.0.0-Rc
  • Commerce Products
    • Commerce Packages
    • Commerce Payment Providers
    • Commerce Shipping Providers
  • Getting Started
    • Requirements
    • Installation
    • Licensing
    • Configuration
    • User Interface
  • Upgrading
    • Upgrading Umbraco Commerce
    • Version Specific Upgrade Notes
    • Migrate from Vendr to Umbraco Commerce
      • Migrate Umbraco Commerce Checkout
      • Migrate custom Payment Providers
  • Tutorials
    • Build a Store in Umbraco using Umbraco Commerce
      • Installation
      • Creating a Store
        • Configuring your Store
      • Creating your first Product
      • Implementing a Shopping Cart
        • Using the Umbraco.Commerce.Cart Drop-in Shopping Cart
        • Creating a Custom Shopping Cart
      • Implementing a Checkout Flow
        • Using the Umbraco.Commerce.Checkout Drop-in Checkout Flow
        • Creating a Custom Checkout Flow
      • Configuring Store Access Permissions
  • How-To Guides
    • Overview
    • Configure SQLite support
    • Use an Alternative Database for Umbraco Commerce Tables
    • Customizing Templates
    • Configuring Cart Cleanup
    • Limit Order Line Quantity
    • Implementing Product Bundles
    • Implementing Member Based Pricing
    • Implementing Dynamically Priced Products
    • Implementing Personalized Products
    • Implementing a Currency Switcher
    • Building a Members Portal
    • Order Number Customization
    • Sending Payment Links to Customers
    • Create an Order via Code
  • Key Concepts
    • Get to know the main features
    • Base Currency
    • Calculators
    • Currency Exchange Rate Service Provider
    • Dependency Injection
    • Discount Rules / Rewards
    • Events
      • List of validation events
      • List of notification events
    • Fluent API
    • Order Calculation State
    • Payment Forms
    • Payment Providers
    • Pipelines
    • Price/Amount Adjustments
    • Price Freezing
    • Product Adapters
    • Product Bundles
    • Product Variants
      • Complex Variants
    • Properties
    • ReadOnly and Writable Entities
    • Sales Tax Providers
    • Search Specifications
    • Settings Objects
    • Shipping Package Factories
    • Shipping Providers
    • Shipping Range/Rate Providers
    • Tax Sources
    • UI Extensions
      • Analytics Widgets
      • Entity Quick Actions
      • Order Line Actions
      • Order Properties
      • Order Collection Properties
      • Order Line Properties
      • Store Menu Items
    • Umbraco Properties
    • Unit of Work
    • Umbraco Commerce Builder
    • Webhooks
  • Reference
    • Stores
    • Shipping
      • Fixed Rate Shipping
      • Dynamic Rate Shipping
      • Realtime Rate Shipping
    • Payments
      • Configure Refunds
      • Issue Refunds
    • Taxes
      • Fixed Tax Rates
      • Calculated Tax Rates
    • Storefront API
      • Endpoints
        • Order
        • Checkout
        • Product
        • Customer
        • Store
        • Currency
        • Country
        • Payment method
        • Shipping method
        • Content
    • Management API
    • Go behind the scenes
    • Telemetry
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
Export as PDF
  1. How-To Guides

Create an Order via Code

Learn how to create an order via code in Umbraco Commerce.

In some cases, such as when importing orders from another system, it may be necessary to create a fully finalized order via code.

The example below demonstrates how to do this. The api variable is an instance of the IUmbracoCommerceApi interface, which is an injectable service accessed via dependency injection.

await api.Uow.ExecuteAsync(async (uow) =>
{
    // Fetch the store
    var store = await api.GetStoreAsync("blendid");
    
    // Create or get the current order
    var order = await api.GetOrCreateCurrentOrderAsync(store.Id).AsWritableAsync(uow);

    // Add product
    var productId = "2fa949e4-2acb-4aef-bb7c-4d3a5f7bbe52"; // The product node Key
    await order.AddProductAsync(productId, null, 1);

    // Set customer details
    var country = await api.GetCountryAsync(store.Id, "GB");
    await order.SetPropertiesAsync(new Dictionary<string, string>
        {
            { Constants.Properties.Customer.EmailPropertyAlias, $"customer@example.com" },
            { "marketingOptIn", "0" },
            { Constants.Properties.Customer.FirstNamePropertyAlias, "Example" },
            { Constants.Properties.Customer.LastNamePropertyAlias, $"Customer" },
            { "billingAddressLine1", "10 Example Road" },
            { "billingCity", "Example City" },
            { "billingZipCode", "EX3 3PL" },
            { "billingTelephone", "0123456789" },
            { "shippingSameAsBilling", "1" }
        })
        .SetPaymentCountryRegionAsync(country, null)
        .SetShippingCountryRegionAsync(country, null);

    // Set shipping method
    var shippingMethod = await api.GetShippingMethodAsync(store.Id, "pickup");
    await order.SetShippingMethodAsync(shippingMethod);

    // Set payment method
    var paymentMethod = await api.GetPaymentMethodAsync(store.Id, "invoicing");
    await order.SetPaymentMethodAsync(paymentMethod);

    // Recalculate order
    await order.RecalculateAsync();

    // Finalize order
    await order.InitializeTransactionAsync();
    await order.FinalizeAsync(order.TransactionAmount.Value, Guid.NewGuid().ToString("N"), PaymentStatus.Authorized);

    // Save order
    await api.SaveOrderAsync(order);

    // Commit the unit of work
    uow.Complete();
});
PreviousSending Payment Links to CustomersNextGet to know the main features

Last updated 2 months ago

Was this helpful?