Skip to content

Commit 9a27887

Browse files
Update shipping method selection page to support shipping options
1 parent 21d23ad commit 9a27887

File tree

4 files changed

+82
-10
lines changed

4 files changed

+82
-10
lines changed

src/Umbraco.Commerce.Checkout/Client/src/js/uccheckout.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(function () {
1+
(function () {
22

33
// Initialization
44
function init() {
@@ -34,6 +34,13 @@
3434
h();
3535
});
3636

37+
// Update shippingOptionId based on selected shipping method
38+
document.querySelectorAll("input[name=shippingMethod]").forEach(el => {
39+
var h = () => { document.querySelector("input[name=shippingOptionId]").value = document.querySelector("input[name=shippingMethod]:checked").dataset.optionId; }
40+
el.addEventListener("change", h);
41+
if (el.checked) h();
42+
});
43+
3744
// Enable / disable continue button when accepting terms
3845
var acceptTermsEl = document.getElementById("accept-terms");
3946
if (acceptTermsEl) {

src/Umbraco.Commerce.Checkout/Views/UmbracoCommerceCheckout/UmbracoCommerceCheckoutShippingMethodPage.cshtml

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
? shippingCountry.DefaultShippingMethodId.Value
3333
: shippingMethods.FirstOrDefault()?.Id;
3434

35+
var currentShippinOptionId = currentShippingMethodId.HasValue && currentOrder.ShippingInfo.ShippingMethodId.HasValue && currentShippingMethodId == currentOrder.ShippingInfo.ShippingMethodId
36+
? currentOrder.ShippingInfo.ShippingOption?.Id
37+
: null;
38+
3539
var checkoutPage = Model.GetCheckoutPage();
3640
var nextStepPage = Model.GetNextStepPage();
3741
}
@@ -44,15 +48,55 @@
4448
<ul class="border border-gray-300 rounded">
4549
@foreach (var item in shippingMethods.Select((sm, i) => new { ShippingMethod = sm, Index = i }))
4650
{
47-
<li class="border-gray-300 @(item.Index > 0 ? "border-t " : "")">
48-
<label class="flex items-center py-4 px-4 cursor-pointer hover:bg-gray-50">
49-
<input name="shippingMethod" type="radio" value="@item.ShippingMethod.Id" class="mr-3" @Html.Raw(currentShippingMethodId.HasValue && currentShippingMethodId.Value == item.ShippingMethod.Id ? "checked=\"checked\"" : "") required />
50-
<span class="font-medium">@(item.ShippingMethod.Name)</span>
51-
<span class="flex-1 text-right">@(item.ShippingMethod.CalculatePrice()?.Formatted())</span>
52-
</label>
53-
</li>
51+
var rates = item.ShippingMethod.TryCalculateRates(currentOrder);
52+
if (rates.Success)
53+
{
54+
foreach (var rate in rates.Result)
55+
{
56+
var isSelected = false;
57+
58+
if (currentShippingMethodId.HasValue && currentShippingMethodId.Value == item.ShippingMethod.Id)
59+
{
60+
if (rate.Option != null)
61+
{
62+
if (currentShippinOptionId == null)
63+
{
64+
currentShippinOptionId = rate.Option?.Id;
65+
isSelected = true;
66+
}
67+
else if (currentShippinOptionId == rate.Option?.Id)
68+
{
69+
isSelected = true;
70+
}
71+
}
72+
else
73+
{
74+
isSelected = true;
75+
}
76+
}
77+
78+
<li class="border-gray-300 @(item.Index > 0 ? "border-t " : "")">
79+
<label class="flex items-center py-4 px-4 cursor-pointer hover:bg-gray-50">
80+
<input name="shippingMethod" type="radio" value="@item.ShippingMethod.Id"
81+
data-option-id="@rate.Option?.Id"
82+
class="mr-3" required
83+
@Html.Raw(isSelected ? " checked=\"checked\"" : "") />
84+
<span class="font-medium">
85+
@(item.ShippingMethod.Name)
86+
@if (rate.Option != null)
87+
{
88+
<text> - @rate.Option.Name</text>
89+
}
90+
</span>
91+
<span class="flex-1 text-right">@(rate.Value?.Formatted())</span>
92+
</label>
93+
</li>
94+
}
95+
}
5496
}
5597
</ul>
98+
99+
<input type="hidden" name="shippingOptionId" value="@(currentShippinOptionId)" />
56100

57101
@Html.Partial("~/Views/UmbracoCommerceCheckout/Partials/UmbracoCommerceCheckoutPrevNext.cshtml")
58102
}

src/Umbraco.Commerce.Checkout/Web/Controllers/UmbracoCommerceCheckoutSurfaceController.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Umbraco.Extensions;
1818

1919
using UmbracoCommerceConstants = Umbraco.Commerce.Core.Constants;
20+
using Umbraco.Commerce.Common.Models;
2021

2122
namespace Umbraco.Commerce.Checkout.Web.Controllers
2223
{
@@ -183,8 +184,26 @@ public IActionResult UpdateOrderShippingMethod(UccUpdateOrderShippingMethodDto m
183184
var checkoutPage = CurrentPage.GetCheckoutPage();
184185
var store = CurrentPage.GetStore();
185186
var order = _commerceApi.GetCurrentOrder(store.Id)
186-
.AsWritable(uow)
187-
.SetShippingMethod(model.ShippingMethod);
187+
.AsWritable(uow);
188+
189+
if (!model.ShippingOptionId.IsNullOrWhiteSpace())
190+
{
191+
var shippingMethod = _commerceApi.GetShippingMethod(model.ShippingMethod);
192+
var shippingRateAttempt = shippingMethod.TryCalculateRate(model.ShippingOptionId, order);
193+
194+
if (shippingRateAttempt.Success)
195+
{
196+
order.SetShippingMethod(model.ShippingMethod, shippingRateAttempt.Result.Option);
197+
}
198+
else
199+
{
200+
throw new ValidationException(new[] { new ValidationError("Unable to locate the selected shipping option") });
201+
}
202+
}
203+
else
204+
{
205+
order.SetShippingMethod(model.ShippingMethod);
206+
}
188207

189208
_commerceApi.SaveOrder(order);
190209

src/Umbraco.Commerce.Checkout/Web/Dtos/UccUpdateOrderShippingMethodDto.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public class UccUpdateOrderShippingMethodDto
66
{
77
public Guid ShippingMethod { get; set; }
88

9+
public string ShippingOptionId { get; set; }
10+
911
public Guid? NextStep { get; set; }
1012
}
1113
}

0 commit comments

Comments
 (0)