Skip to content

Commit 484cb93

Browse files
authored
#1088: Bug with special product price when ordering (#1089)
1 parent 444c7a5 commit 484cb93

File tree

10 files changed

+61
-11
lines changed

10 files changed

+61
-11
lines changed

src/Modules/SimplCommerce.Module.Checkouts/Areas/Checkouts/ViewModels/CheckoutItemVm.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public CheckoutItemVm(ICurrencyService currencyService)
2626

2727
public string ProductImage { get; set; }
2828

29+
public CalculatedProductPrice CalculatedProductPrice { get; set; }
30+
2931
public decimal ProductPrice { get; set; }
3032

3133
public string ProductPriceString => _currencyService.FormatCurrency(ProductPrice);

src/Modules/SimplCommerce.Module.Checkouts/Services/CheckoutService.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.EntityFrameworkCore;
66
using Microsoft.Extensions.Configuration;
77
using SimplCommerce.Infrastructure.Data;
8+
using SimplCommerce.Module.Catalog.Services;
89
using SimplCommerce.Module.Checkouts.Areas.Checkouts.ViewModels;
910
using SimplCommerce.Module.Checkouts.Models;
1011
using SimplCommerce.Module.Core.Models;
@@ -26,6 +27,7 @@ public class CheckoutService : ICheckoutService
2627
private readonly IMediaService _mediaService;
2728
private readonly ICouponService _couponService;
2829
private readonly bool _isProductPriceIncludeTax;
30+
private readonly IProductPricingService _productPriceService;
2931

3032
public CheckoutService(
3133
IRepositoryWithTypedId<Checkout, Guid> checkoutRepository,
@@ -36,7 +38,8 @@ public CheckoutService(
3638
ICurrencyService currencyService,
3739
IMediaService mediaService,
3840
ICouponService couponService,
39-
IConfiguration config)
41+
IConfiguration config,
42+
IProductPricingService productPriceService)
4043
{
4144
_checkoutRepository = checkoutRepository;
4245
_userAddressRepository = userAddressRepository;
@@ -47,6 +50,7 @@ public CheckoutService(
4750
_mediaService = mediaService;
4851
_couponService = couponService;
4952
_isProductPriceIncludeTax = config.GetValue<bool>("Catalog.IsProductPriceIncludeTax");
53+
_productPriceService = productPriceService;
5054
}
5155

5256
public async Task<Checkout> Create(long customerId, long createdById, IList<CartItemToCheckoutVm> cartItems, string couponCode)
@@ -182,6 +186,7 @@ public async Task<CheckoutVm> GetCheckoutDetails(Guid checkoutId)
182186
Id = x.Id,
183187
ProductId = x.ProductId,
184188
ProductName = x.Product.Name,
189+
CalculatedProductPrice = _productPriceService.CalculateProductPrice(x.Product),
185190
ProductPrice = x.Product.Price,
186191
ProductStockQuantity = x.Product.StockQuantity,
187192
ProductStockTrackingIsEnabled = x.Product.StockTrackingIsEnabled,
@@ -191,7 +196,7 @@ public async Task<CheckoutVm> GetCheckoutDetails(Guid checkoutId)
191196
VariationOptions = CheckoutItemVm.GetVariationOption(x.Product)
192197
}).ToList();
193198

194-
checkoutVm.SubTotal = checkoutVm.Items.Sum(x => x.Quantity * x.ProductPrice);
199+
checkoutVm.SubTotal = checkoutVm.Items.Sum(x => x.Quantity * (x.CalculatedProductPrice.OldPrice ?? x.ProductPrice));
195200
if (!string.IsNullOrWhiteSpace(checkoutVm.CouponCode))
196201
{
197202
var cartInfoForCoupon = new CartInfoForCoupon
@@ -209,6 +214,10 @@ public async Task<CheckoutVm> GetCheckoutDetails(Guid checkoutId)
209214
}
210215
}
211216

217+
checkoutVm.Discount += checkoutVm.Items
218+
.Where(x => x.CalculatedProductPrice.OldPrice.HasValue)
219+
.Sum(x => x.Quantity * (x.CalculatedProductPrice.OldPrice.Value - x.CalculatedProductPrice.Price));
220+
212221
return checkoutVm;
213222
}
214223
}

src/Modules/SimplCommerce.Module.Orders/Areas/Orders/Views/Shared/Components/OrderSummary/Default.cshtml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,16 @@
2323
}
2424
</td>
2525
<td class="text-center">@item.Quantity</td>
26-
<td class="text-right">@item.ProductPriceString</td>
26+
<td class="text-right">
27+
@if(item.CalculatedProductPrice.OldPrice.HasValue)
28+
{
29+
<text>@item.CalculatedProductPrice.OldPriceString</text>
30+
}
31+
else
32+
{
33+
<text>@item.CalculatedProductPrice.PriceString</text>
34+
}
35+
</td>
2736
</tr>
2837
}
2938
<tr>

src/Modules/SimplCommerce.Module.Orders/Services/OrderService.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using SimplCommerce.Module.Checkouts.Services;
1919
using SimplCommerce.Module.Checkouts.Models;
2020
using SimplCommerce.Module.Checkouts.Areas.Checkouts.ViewModels;
21+
using SimplCommerce.Module.Catalog.Services;
2122

2223
namespace SimplCommerce.Module.Orders.Services
2324
{
@@ -33,6 +34,7 @@ public class OrderService : IOrderService
3334
private readonly IShippingPriceService _shippingPriceService;
3435
private readonly IRepository<UserAddress> _userAddressRepository;
3536
private readonly IMediator _mediator;
37+
private readonly IProductPricingService _productPricingService;
3638

3739
public OrderService(IRepository<Order> orderRepository,
3840
ICouponService couponService,
@@ -43,7 +45,8 @@ public OrderService(IRepository<Order> orderRepository,
4345
IRepositoryWithTypedId<Checkout, Guid> checkoutRepository,
4446
IShippingPriceService shippingPriceService,
4547
IRepository<UserAddress> userAddressRepository,
46-
IMediator mediator)
48+
IMediator mediator,
49+
IProductPricingService productPricingService)
4750
{
4851
_orderRepository = orderRepository;
4952
_couponService = couponService;
@@ -55,6 +58,7 @@ public OrderService(IRepository<Order> orderRepository,
5558
_shippingPriceService = shippingPriceService;
5659
_userAddressRepository = userAddressRepository;
5760
_mediator = mediator;
61+
_productPricingService = productPricingService;
5862
}
5963

6064
public async Task<Result<Order>> CreateOrder(Guid checkoutId, string paymentMethod, decimal paymentFeeAmount, OrderStatus orderStatus = OrderStatus.New)
@@ -220,7 +224,10 @@ public async Task<Result<Order>> CreateOrder(Guid checkoutId, string paymentMeth
220224
}
221225

222226
var taxPercent = await _taxService.GetTaxPercent(checkoutItem.Product.TaxClassId, shippingAddress.CountryId, shippingAddress.StateOrProvinceId, shippingAddress.ZipCode);
223-
var productPrice = checkoutItem.Product.Price;
227+
228+
var calculatedProductPrice = _productPricingService.CalculateProductPrice(checkoutItem.Product);
229+
230+
var productPrice = calculatedProductPrice.OldPrice ?? calculatedProductPrice.Price;
224231
if (checkout.IsProductPriceIncludeTax)
225232
{
226233
productPrice = productPrice / (1 + (taxPercent / 100));
@@ -241,6 +248,11 @@ public async Task<Result<Order>> CreateOrder(Guid checkoutId, string paymentMeth
241248
orderItem.DiscountAmount = discountedItem.DiscountAmount;
242249
}
243250

251+
if (calculatedProductPrice.OldPrice.HasValue)
252+
{
253+
orderItem.DiscountAmount += orderItem.Quantity * (calculatedProductPrice.OldPrice.Value - calculatedProductPrice.Price);
254+
}
255+
244256
order.AddOrderItem(orderItem);
245257
if (checkoutItem.Product.StockTrackingIsEnabled)
246258
{
@@ -252,7 +264,7 @@ public async Task<Result<Order>> CreateOrder(Guid checkoutId, string paymentMeth
252264
order.OrderNote = checkout.OrderNote;
253265
order.CouponCode = checkingDiscountResult.CouponCode;
254266
order.CouponRuleName = checkout.CouponRuleName;
255-
order.DiscountAmount = checkingDiscountResult.DiscountAmount;
267+
order.DiscountAmount = checkingDiscountResult.DiscountAmount + order.OrderItems.Sum(x => x.DiscountAmount);
256268
order.ShippingFeeAmount = shippingMethod.Price;
257269
order.ShippingMethod = shippingMethod.Name;
258270
order.TaxAmount = order.OrderItems.Sum(x => x.TaxAmount);

src/Modules/SimplCommerce.Module.ShoppingCart/Areas/ShoppingCart/Controllers/CartController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public async Task<IActionResult> AddToCartResult(long productId)
7272
model.ProductName = addedProduct.ProductName;
7373
model.ProductImage = addedProduct.ProductImage;
7474
model.ProductPrice = addedProduct.ProductPrice;
75+
model.CalculatedProductPrice = addedProduct.CalculatedProductPrice;
7576
model.Quantity = addedProduct.Quantity;
7677

7778
return PartialView(model);

src/Modules/SimplCommerce.Module.ShoppingCart/Areas/ShoppingCart/ViewModels/AddToCartResultVm.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using SimplCommerce.Module.Core.Services;
1+
using SimplCommerce.Module.Catalog.Models;
2+
using SimplCommerce.Module.Core.Services;
23

34
namespace SimplCommerce.Module.ShoppingCart.Areas.ShoppingCart.ViewModels
45
{
@@ -15,6 +16,8 @@ public AddToCartResultVm(ICurrencyService currencyService)
1516

1617
public string ProductImage { get; set; }
1718

19+
public CalculatedProductPrice CalculatedProductPrice { get; set; }
20+
1821
public decimal ProductPrice { get; set; }
1922

2023
public string VariationName { get; set; }

src/Modules/SimplCommerce.Module.ShoppingCart/Areas/ShoppingCart/ViewModels/CartItemVm.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public CartItemVm(ICurrencyService currencyService)
2222

2323
public string ProductImage { get; set; }
2424

25+
public CalculatedProductPrice CalculatedProductPrice { get; set; }
26+
2527
public decimal ProductPrice { get; set; }
2628

2729
public string ProductPriceString => _currencyService.FormatCurrency(ProductPrice);

src/Modules/SimplCommerce.Module.ShoppingCart/Areas/ShoppingCart/Views/Cart/AddToCartResult.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<h6>@Model.VariationName</h6>
1515
</div>
1616
<div class="col-md-3">
17-
@Model.Quantity x @Model.ProductPriceString
17+
@Model.Quantity x @Model.CalculatedProductPrice.PriceString
1818
</div>
1919
</div>
2020
<div class="row">

src/Modules/SimplCommerce.Module.ShoppingCart/Areas/ShoppingCart/Views/Cart/Index.cshtml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
<span ng-if="!cartItem.isProductAvailabeToOrder" class="badge badge-pill badge-danger">@Localizer["Not availabe any more"]</span>
2727
<span ng-if="cartItem.productStockTrackingIsEnabled && cartItem.productStockQuantity < cartItem.quantity" class="badge badge-pill badge-danger">@Localizer["Not enough stock. Available:"] {{cartItem.productStockQuantity}}</span>
2828
</td>
29-
<td class="text-right">{{cartItem.productPriceString}}</td>
29+
<td class="text-right">
30+
{{cartItem.calculatedProductPrice.priceString}}
31+
<br />
32+
<del ng-if="cartItem.calculatedProductPrice.oldPrice > cartItem.calculatedProductPrice.price">{{cartItem.calculatedProductPrice.oldPriceString}}</del>
33+
</td>
3034
<td class="text-center">
3135
<button type="button" class="quantity-button" ng-click="vm.decreaseQuantity(cartItem)" name="subtract" value="-">-</button>
3236
<input type="text" class="quantity-field" ng-model="cartItem.quantity" readonly="readonly" />

src/Modules/SimplCommerce.Module.ShoppingCart/Services/CartService.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using SimplCommerce.Module.Pricing.Services;
1111
using SimplCommerce.Module.ShoppingCart.Areas.ShoppingCart.ViewModels;
1212
using Microsoft.Extensions.Localization;
13+
using SimplCommerce.Module.Catalog.Services;
1314

1415
namespace SimplCommerce.Module.ShoppingCart.Services
1516
{
@@ -21,16 +22,18 @@ public class CartService : ICartService
2122
private readonly bool _isProductPriceIncludeTax;
2223
private readonly ICurrencyService _currencyService;
2324
private readonly IStringLocalizer _localizer;
25+
private readonly IProductPricingService _productPricingService;
2426

2527
public CartService(IRepository<CartItem> cartItemRepository, ICouponService couponService,
26-
IMediaService mediaService, IConfiguration config, ICurrencyService currencyService, IStringLocalizerFactory stringLocalizerFactory)
28+
IMediaService mediaService, IConfiguration config, ICurrencyService currencyService, IStringLocalizerFactory stringLocalizerFactory, IProductPricingService productPricingService)
2729
{
2830
_cartItemRepository = cartItemRepository;
2931
_couponService = couponService;
3032
_mediaService = mediaService;
3133
_currencyService = currencyService;
3234
_isProductPriceIncludeTax = config.GetValue<bool>("Catalog.IsProductPriceIncludeTax");
3335
_localizer = stringLocalizerFactory.Create(null);
36+
_productPricingService = productPricingService;
3437
}
3538

3639
public async Task<AddToCartResult> AddToCart(long customerId, long productId, int quantity)
@@ -91,6 +94,7 @@ public async Task<CartVm> GetCartDetails(long customerId)
9194
ProductId = x.ProductId,
9295
ProductName = x.Product.Name,
9396
ProductPrice = x.Product.Price,
97+
CalculatedProductPrice = _productPricingService.CalculateProductPrice(x.Product),
9498
ProductStockQuantity = x.Product.StockQuantity,
9599
ProductStockTrackingIsEnabled = x.Product.StockTrackingIsEnabled,
96100
IsProductAvailabeToOrder = x.Product.IsAllowToOrder && x.Product.IsPublished && !x.Product.IsDeleted,
@@ -99,7 +103,7 @@ public async Task<CartVm> GetCartDetails(long customerId)
99103
VariationOptions = CartItemVm.GetVariationOption(x.Product)
100104
}).ToList();
101105

102-
cartVm.SubTotal = cartVm.Items.Sum(x => x.Quantity * x.ProductPrice);
106+
cartVm.SubTotal = cartVm.Items.Sum(x => x.Quantity * (x.CalculatedProductPrice.OldPrice ?? x.ProductPrice));
103107
if (!string.IsNullOrWhiteSpace(cartVm.CouponCode))
104108
{
105109
var cartInfoForCoupon = new CartInfoForCoupon
@@ -117,6 +121,10 @@ public async Task<CartVm> GetCartDetails(long customerId)
117121
}
118122
}
119123

124+
cartVm.Discount += cartVm.Items
125+
.Where(x => x.CalculatedProductPrice.OldPrice.HasValue)
126+
.Sum(x => x.Quantity * (x.CalculatedProductPrice.OldPrice.Value - x.CalculatedProductPrice.Price));
127+
120128
return cartVm;
121129
}
122130

0 commit comments

Comments
 (0)