From 10393ff30307b0c54e8c038fb35830672921cd36 Mon Sep 17 00:00:00 2001 From: Matt Brailsford Date: Tue, 8 Apr 2025 09:36:51 +0100 Subject: [PATCH 1/4] Initial commit --- 15/umbraco-commerce/SUMMARY.md | 1 + 15/umbraco-commerce/how-to-guides/create-order-via-code.md | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 15/umbraco-commerce/how-to-guides/create-order-via-code.md diff --git a/15/umbraco-commerce/SUMMARY.md b/15/umbraco-commerce/SUMMARY.md index 9d6c042cd78..8852f839981 100644 --- a/15/umbraco-commerce/SUMMARY.md +++ b/15/umbraco-commerce/SUMMARY.md @@ -57,6 +57,7 @@ * [Implementing a Currency Switcher](how-to-guides/currency-switching.md) * [Building a Members Portal](how-to-guides/member-portal.md) * [Order Number Customization](how-to-guides/order-number-customization.md) +* [Create an Order via Code](how-to-guides/create-order-via-code.md) ## Key Concepts diff --git a/15/umbraco-commerce/how-to-guides/create-order-via-code.md b/15/umbraco-commerce/how-to-guides/create-order-via-code.md new file mode 100644 index 00000000000..6b6cebb2051 --- /dev/null +++ b/15/umbraco-commerce/how-to-guides/create-order-via-code.md @@ -0,0 +1,5 @@ +--- +description: Learn how to create an order via code in Umbraco Commerce. +--- + +# Create an Order via Code \ No newline at end of file From a4ed98b3a16309d8b16f4d3add7ff206f6e7547d Mon Sep 17 00:00:00 2001 From: mattbrailsford Date: Tue, 8 Apr 2025 10:05:30 +0100 Subject: [PATCH 2/4] Added code example for creating an order via code --- .../how-to-guides/create-order-via-code.md | 59 ++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/15/umbraco-commerce/how-to-guides/create-order-via-code.md b/15/umbraco-commerce/how-to-guides/create-order-via-code.md index 6b6cebb2051..9ce4df8f02a 100644 --- a/15/umbraco-commerce/how-to-guides/create-order-via-code.md +++ b/15/umbraco-commerce/how-to-guides/create-order-via-code.md @@ -2,4 +2,61 @@ description: Learn how to create an order via code in Umbraco Commerce. --- -# Create an Order via Code \ No newline at end of file +# Create an Order via Code + +It is sometimes necessary, such as when importing orders from another system, to create a fully finalized order via code. + +An example of the code to do this is shown below. Here the `api` variable is an instance of the `IUmbracoCommerceApi` interface, which is an injectable service you access via [dependency injection](../key-concepts/dependency-injection.md). + +```csharp +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 + { + { 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(); +}); +``` From 145bc5bd8f7dd2fb9cf025dc9c1a90c2aabb2bd8 Mon Sep 17 00:00:00 2001 From: Esha Noronha <82437098+eshanrnh@users.noreply.github.com> Date: Tue, 8 Apr 2025 15:19:42 +0200 Subject: [PATCH 3/4] Update 15/umbraco-commerce/how-to-guides/create-order-via-code.md --- 15/umbraco-commerce/how-to-guides/create-order-via-code.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/15/umbraco-commerce/how-to-guides/create-order-via-code.md b/15/umbraco-commerce/how-to-guides/create-order-via-code.md index 9ce4df8f02a..13816ed8dc4 100644 --- a/15/umbraco-commerce/how-to-guides/create-order-via-code.md +++ b/15/umbraco-commerce/how-to-guides/create-order-via-code.md @@ -4,7 +4,7 @@ description: Learn how to create an order via code in Umbraco Commerce. # Create an Order via Code -It is sometimes necessary, such as when importing orders from another system, to create a fully finalized order via code. +In some cases, such as when importing orders from another system, it may be necessary to create a fully finalized order via code. An example of the code to do this is shown below. Here the `api` variable is an instance of the `IUmbracoCommerceApi` interface, which is an injectable service you access via [dependency injection](../key-concepts/dependency-injection.md). From 906179377bac2b3d61135112b696e60a1342a5ef Mon Sep 17 00:00:00 2001 From: Esha Noronha <82437098+eshanrnh@users.noreply.github.com> Date: Tue, 8 Apr 2025 15:19:52 +0200 Subject: [PATCH 4/4] Update 15/umbraco-commerce/how-to-guides/create-order-via-code.md --- 15/umbraco-commerce/how-to-guides/create-order-via-code.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/15/umbraco-commerce/how-to-guides/create-order-via-code.md b/15/umbraco-commerce/how-to-guides/create-order-via-code.md index 13816ed8dc4..134348bc8c5 100644 --- a/15/umbraco-commerce/how-to-guides/create-order-via-code.md +++ b/15/umbraco-commerce/how-to-guides/create-order-via-code.md @@ -6,7 +6,7 @@ description: 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. -An example of the code to do this is shown below. Here the `api` variable is an instance of the `IUmbracoCommerceApi` interface, which is an injectable service you access via [dependency injection](../key-concepts/dependency-injection.md). +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](../key-concepts/dependency-injection.md). ```csharp await api.Uow.ExecuteAsync(async (uow) =>