Skip to content

Commit 539da63

Browse files
committed
Resolves smartstore#1479 Save delivery time and maybe stock info in OrderItem table
1 parent 462b746 commit 539da63

File tree

13 files changed

+293
-54
lines changed

13 files changed

+293
-54
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
### Improvements
1717
* (Perf) Significantly increased query performance for products with a lot of category assignments (> 10).
1818
* Debitoor: Partially update customer instead of full update to avoid all fields being overwritten
19+
* #1479 Show in messages the delivery time at the time of purchase
1920

2021
### Bugfixes
2122
* In a multi-store environment, multiple topics with the same system name cannot be resolved reliably.

src/Libraries/SmartStore.Core/Domain/Orders/OrderItem.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ public partial class OrderItem : BaseEntity
128128
[DataMember]
129129
public decimal ProductCost { get; set; }
130130

131+
/// <summary>
132+
/// Gets or sets the delivery time at the time of purchase.
133+
/// </summary>
134+
[DataMember]
135+
public int? DeliveryTimeId { get; set; }
136+
137+
/// <summary>
138+
/// Indicates whether the delivery time was displayed at the time of purchase.
139+
/// </summary>
140+
[DataMember]
141+
public bool DisplayDeliveryTime { get; set; }
142+
131143
/// <summary>
132144
/// Gets the order
133145
/// </summary>

src/Libraries/SmartStore.Data/Migrations/201807191020207_OrderItemDeliveryTime.Designer.cs

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace SmartStore.Data.Migrations
2+
{
3+
using System;
4+
using System.Data.Entity.Migrations;
5+
6+
public partial class OrderItemDeliveryTime : DbMigration
7+
{
8+
public override void Up()
9+
{
10+
AddColumn("dbo.OrderItem", "DeliveryTimeId", c => c.Int());
11+
AddColumn("dbo.OrderItem", "DisplayDeliveryTime", c => c.Boolean(nullable: false));
12+
}
13+
14+
public override void Down()
15+
{
16+
DropColumn("dbo.OrderItem", "DisplayDeliveryTime");
17+
DropColumn("dbo.OrderItem", "DeliveryTimeId");
18+
}
19+
}
20+
}

src/Libraries/SmartStore.Data/Migrations/201807191020207_OrderItemDeliveryTime.resx

Lines changed: 126 additions & 0 deletions
Large diffs are not rendered by default.

src/Libraries/SmartStore.Data/SmartStore.Data.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,10 @@
603603
<Compile Include="Migrations\201807122120062_TopicAcl.Designer.cs">
604604
<DependentUpon>201807122120062_TopicAcl.cs</DependentUpon>
605605
</Compile>
606+
<Compile Include="Migrations\201807191020207_OrderItemDeliveryTime.cs" />
607+
<Compile Include="Migrations\201807191020207_OrderItemDeliveryTime.Designer.cs">
608+
<DependentUpon>201807191020207_OrderItemDeliveryTime.cs</DependentUpon>
609+
</Compile>
606610
<Compile Include="ObjectContextBase.SaveChanges.cs" />
607611
<Compile Include="Setup\Builder\ActivityLogTypeMigrator.cs" />
608612
<Compile Include="Setup\Builder\PermissionMigrator.cs" />
@@ -1085,6 +1089,9 @@
10851089
<EmbeddedResource Include="Migrations\201807122120062_TopicAcl.resx">
10861090
<DependentUpon>201807122120062_TopicAcl.cs</DependentUpon>
10871091
</EmbeddedResource>
1092+
<EmbeddedResource Include="Migrations\201807191020207_OrderItemDeliveryTime.resx">
1093+
<DependentUpon>201807191020207_OrderItemDeliveryTime.cs</DependentUpon>
1094+
</EmbeddedResource>
10881095
<EmbeddedResource Include="Sql\Indexes.sql" />
10891096
<EmbeddedResource Include="Sql\StoredProcedures.sql" />
10901097
</ItemGroup>

src/Libraries/SmartStore.Services/Catalog/ProductExtensions.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,9 @@ public static string FormatStockMessage(this Product product, ILocalizationServi
199199
public static bool DisplayDeliveryTimeAccordingToStock(this Product product, CatalogSettings catalogSettings)
200200
{
201201
Guard.NotNull(product, nameof(product));
202+
Guard.NotNull(catalogSettings, nameof(catalogSettings));
202203

203-
if (product.ManageInventoryMethod == ManageInventoryMethod.ManageStock || product.ManageInventoryMethod == ManageInventoryMethod.ManageStockByAttributes)
204+
if (product.ManageInventoryMethod == ManageInventoryMethod.ManageStock || product.ManageInventoryMethod == ManageInventoryMethod.ManageStockByAttributes)
204205
{
205206
if (catalogSettings.DeliveryTimeIdForEmptyStock.HasValue && product.StockQuantity <= 0)
206207
return true;
@@ -211,13 +212,32 @@ public static bool DisplayDeliveryTimeAccordingToStock(this Product product, Cat
211212
return true;
212213
}
213214

214-
/// <summary>
215-
/// Indicates whether the product is labeled as NEW.
216-
/// </summary>
217-
/// <param name="product">Product entity</param>
218-
/// <param name="catalogSettings">Catalog settings</param>
219-
/// <returns>Whether the product is labeled as NEW</returns>
220-
public static bool IsNew(this Product product, CatalogSettings catalogSettings)
215+
public static int? GetDeliveryTimeIdAccordingToStock(this Product product, CatalogSettings catalogSettings)
216+
{
217+
Guard.NotNull(catalogSettings, nameof(catalogSettings));
218+
219+
if (product == null)
220+
{
221+
return null;
222+
}
223+
224+
if ((product.ManageInventoryMethod == ManageInventoryMethod.ManageStock || product.ManageInventoryMethod == ManageInventoryMethod.ManageStockByAttributes)
225+
&& catalogSettings.DeliveryTimeIdForEmptyStock.HasValue
226+
&& product.StockQuantity <= 0)
227+
{
228+
return catalogSettings.DeliveryTimeIdForEmptyStock.Value;
229+
}
230+
231+
return product.DeliveryTimeId;
232+
}
233+
234+
/// <summary>
235+
/// Indicates whether the product is labeled as NEW.
236+
/// </summary>
237+
/// <param name="product">Product entity</param>
238+
/// <param name="catalogSettings">Catalog settings</param>
239+
/// <returns>Whether the product is labeled as NEW</returns>
240+
public static bool IsNew(this Product product, CatalogSettings catalogSettings)
221241
{
222242
if (catalogSettings.LabelAsNewForMaxDays.HasValue)
223243
{

src/Libraries/SmartStore.Services/Directory/DeliveryTimeService.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
using SmartStore.Core.Localization;
99
using SmartStore.Core.Plugins;
1010
using SmartStore.Data.Caching;
11+
using SmartStore.Services.Catalog;
1112
using SmartStore.Services.Customers;
1213

1314
namespace SmartStore.Services.Directory
1415
{
15-
public partial class DeliveryTimeService : IDeliveryTimeService
16+
public partial class DeliveryTimeService : IDeliveryTimeService
1617
{
1718
private readonly IRepository<DeliveryTime> _deliveryTimeRepository;
1819
private readonly IRepository<Product> _productRepository;
@@ -85,18 +86,10 @@ public virtual DeliveryTime GetDeliveryTimeById(int deliveryTimeId)
8586
return _deliveryTimeRepository.GetByIdCached(deliveryTimeId, "deliverytime-{0}".FormatInvariant(deliveryTimeId));
8687
}
8788

88-
public virtual DeliveryTime GetDeliveryTime(Product product)
89+
public virtual DeliveryTime GetDeliveryTime(Product product)
8990
{
90-
if (product == null)
91-
return null;
92-
93-
if ((product.ManageInventoryMethod == ManageInventoryMethod.ManageStock || product.ManageInventoryMethod == ManageInventoryMethod.ManageStockByAttributes)
94-
&& _catalogSettings.DeliveryTimeIdForEmptyStock.HasValue && product.StockQuantity <= 0)
95-
{
96-
return GetDeliveryTimeById(_catalogSettings.DeliveryTimeIdForEmptyStock.Value);
97-
}
98-
99-
return GetDeliveryTimeById(product.DeliveryTimeId ?? 0);
91+
var deliveryTimeId = product.GetDeliveryTimeIdAccordingToStock(_catalogSettings);
92+
return GetDeliveryTimeById(deliveryTimeId ?? 0);
10093
}
10194

10295
public virtual IList<DeliveryTime> GetAllDeliveryTimes()

src/Libraries/SmartStore.Services/Messages/MessageModelProvider.OrderParts.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using SmartStore.ComponentModel;
66
using SmartStore.Core.Domain.Catalog;
7+
using SmartStore.Core.Domain.Directory;
78
using SmartStore.Core.Domain.Orders;
89
using SmartStore.Core.Domain.Shipping;
910
using SmartStore.Core.Domain.Tax;
@@ -266,7 +267,8 @@ protected virtual object CreateModelPart(OrderItem part, MessageContext messageC
266267

267268
var productAttributeParser = _services.Resolve<IProductAttributeParser>();
268269
var downloadService = _services.Resolve<IDownloadService>();
269-
var order = part.Order;
270+
var deliveryTimeService = _services.Resolve<IDeliveryTimeService>();
271+
var order = part.Order;
270272
var isNet = order.CustomerTaxDisplayType == TaxDisplayType.ExcludingTax;
271273
var product = part.Product;
272274
product.MergeWithCombination(part.AttributesXml, productAttributeParser);
@@ -304,8 +306,21 @@ protected virtual object CreateModelPart(OrderItem part, MessageContext messageC
304306
{ "LineTotal", FormatPrice(isNet ? part.PriceExclTax : part.PriceInclTax, part.Order, messageContext) },
305307
{ "Product", CreateModelPart(product, messageContext, part.AttributesXml) },
306308
{ "BundleItems", bundleItems },
307-
{ "IsGross", !isNet }
308-
};
309+
{ "IsGross", !isNet },
310+
{ "DisplayDeliveryTime", part.DisplayDeliveryTime },
311+
};
312+
313+
if (part.DeliveryTimeId.HasValue)
314+
{
315+
if (deliveryTimeService.GetDeliveryTimeById(part.DeliveryTimeId ?? 0) is DeliveryTime dt)
316+
{
317+
m["DeliveryTime"] = new Dictionary<string, object>
318+
{
319+
{ "Color", dt.ColorHexValue },
320+
{ "Name", dt.GetLocalized(x => x.Name, messageContext.Language).Value },
321+
};
322+
}
323+
}
309324

310325
PublishModelPartCreatedEvent<OrderItem>(part, m);
311326

src/Libraries/SmartStore.Services/Orders/OrderProcessingService.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,13 @@ public partial class OrderProcessingService : IOrderProcessingService
7373
private readonly LocalizationSettings _localizationSettings;
7474
private readonly CurrencySettings _currencySettings;
7575
private readonly ShoppingCartSettings _shoppingCartSettings;
76+
private readonly CatalogSettings _catalogSettings;
7677

77-
#endregion
78+
#endregion
7879

79-
#region Ctor
80+
#region Ctor
8081

81-
public OrderProcessingService(
82+
public OrderProcessingService(
8283
IOrderService orderService,
8384
IWebHelper webHelper,
8485
ILocalizationService localizationService,
@@ -114,7 +115,8 @@ public OrderProcessingService(
114115
TaxSettings taxSettings,
115116
LocalizationSettings localizationSettings,
116117
CurrencySettings currencySettings,
117-
ShoppingCartSettings shoppingCartSettings)
118+
ShoppingCartSettings shoppingCartSettings,
119+
CatalogSettings catalogSettings)
118120
{
119121
_orderService = orderService;
120122
_webHelper = webHelper;
@@ -152,6 +154,7 @@ public OrderProcessingService(
152154
_localizationSettings = localizationSettings;
153155
_currencySettings = currencySettings;
154156
_shoppingCartSettings = shoppingCartSettings;
157+
_catalogSettings = catalogSettings;
155158

156159
T = NullLocalizer.Instance;
157160
Logger = NullLogger.Instance;
@@ -1030,12 +1033,12 @@ public virtual PlaceOrderResult PlaceOrder(
10301033

10311034
if (!processPaymentRequest.IsRecurringPayment)
10321035
{
1033-
// Move shopping cart items to order products
1036+
// Move shopping cart items to order products.
10341037
foreach (var sc in cart)
10351038
{
10361039
sc.Item.Product.MergeWithCombination(sc.Item.AttributesXml);
10371040

1038-
// Prices
1041+
// Prices.
10391042
decimal taxRate = decimal.Zero;
10401043
decimal unitPriceTaxRate = decimal.Zero;
10411044
decimal scUnitPrice = _priceCalculationService.GetUnitPrice(sc, true);
@@ -1045,7 +1048,7 @@ public virtual PlaceOrderResult PlaceOrder(
10451048
decimal scSubTotalInclTax = _taxService.GetProductPrice(sc.Item.Product, scSubTotal, true, customer, out taxRate);
10461049
decimal scSubTotalExclTax = _taxService.GetProductPrice(sc.Item.Product, scSubTotal, false, customer, out taxRate);
10471050

1048-
// Discounts
1051+
// Discounts.
10491052
Discount scDiscount = null;
10501053
decimal discountAmount = _priceCalculationService.GetDiscountAmount(sc, out scDiscount);
10511054
decimal discountAmountInclTax = _taxService.GetProductPrice(sc.Item.Product, discountAmount, true, customer, out taxRate);
@@ -1056,12 +1059,15 @@ public virtual PlaceOrderResult PlaceOrder(
10561059
appliedDiscounts.Add(scDiscount);
10571060
}
10581061

1059-
// Attributes
10601062
var attributeDescription = _productAttributeFormatter.FormatAttributes(sc.Item.Product, sc.Item.AttributesXml, customer);
1061-
10621063
var itemWeight = _shippingService.GetShoppingCartItemWeight(sc);
1064+
var displayDeliveryTime =
1065+
_shoppingCartSettings.ShowDeliveryTimes &&
1066+
sc.Item.Product.DeliveryTimeId.HasValue &&
1067+
sc.Item.Product.IsShipEnabled &&
1068+
sc.Item.Product.DisplayDeliveryTimeAccordingToStock(_catalogSettings);
10631069

1064-
// Dave order item
1070+
// Save order item.
10651071
var orderItem = new OrderItem
10661072
{
10671073
OrderItemGuid = Guid.NewGuid(),
@@ -1081,10 +1087,12 @@ public virtual PlaceOrderResult PlaceOrder(
10811087
IsDownloadActivated = false,
10821088
LicenseDownloadId = 0,
10831089
ItemWeight = itemWeight,
1084-
ProductCost = _priceCalculationService.GetProductCost(sc.Item.Product, sc.Item.AttributesXml)
1090+
ProductCost = _priceCalculationService.GetProductCost(sc.Item.Product, sc.Item.AttributesXml),
1091+
DeliveryTimeId = sc.Item.Product.GetDeliveryTimeIdAccordingToStock(_catalogSettings),
1092+
DisplayDeliveryTime = displayDeliveryTime
10851093
};
10861094

1087-
if (sc.Item.Product.ProductType == ProductType.BundledProduct && sc.ChildItems != null)
1095+
if (sc.Item.Product.ProductType == ProductType.BundledProduct && sc.ChildItems != null)
10881096
{
10891097
var listBundleData = new List<ProductBundleItemOrderData>();
10901098

@@ -1172,7 +1180,9 @@ public virtual PlaceOrderResult PlaceOrder(
11721180
LicenseDownloadId = 0,
11731181
ItemWeight = orderItem.ItemWeight,
11741182
BundleData = orderItem.BundleData,
1175-
ProductCost = orderItem.ProductCost
1183+
ProductCost = orderItem.ProductCost,
1184+
DeliveryTimeId = orderItem.DeliveryTimeId,
1185+
DisplayDeliveryTime = orderItem.DisplayDeliveryTime
11761186
};
11771187
order.OrderItems.Add(newOrderItem);
11781188
_orderService.UpdateOrder(order);

0 commit comments

Comments
 (0)