Skip to content

Commit

Permalink
Resolves #1143 Make shipping methods suitable for multi-stores
Browse files Browse the repository at this point in the history
  • Loading branch information
mgesing committed Feb 8, 2018
1 parent babd006 commit 2872ff8
Show file tree
Hide file tree
Showing 21 changed files with 331 additions and 81 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* #1285 Copy product: Add option to add more than one copy
* (Perf) Many improvements in hooking framework
* #1294 Swiss PostFinance: External payment page too small on mobile devices. Added setting for mobile device template URL, pre-configured with PostFinance template.
* #1143 Make shipping methods suitable for multi-stores

### Bugfixes
* #1268 Data importer always inserts new pictures and does not detect equal pictures while importing
Expand Down
20 changes: 15 additions & 5 deletions src/Libraries/SmartStore.Core/Domain/Shipping/ShippingMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
using System.Runtime.Serialization;
using SmartStore.Core.Domain.Directory;
using SmartStore.Core.Domain.Localization;
using SmartStore.Core.Domain.Stores;

namespace SmartStore.Core.Domain.Shipping
{
/// <summary>
/// Represents a shipping method (used for offline shipping rate computation methods)
/// </summary>
[DataContract]
public partial class ShippingMethod : BaseEntity, ILocalizedEntity
{
public partial class ShippingMethod : BaseEntity, ILocalizedEntity, IStoreMappingSupported
{
private ICollection<Country> _restrictedCountries;

/// <summary>
Expand All @@ -31,12 +32,21 @@ public partial class ShippingMethod : BaseEntity, ILocalizedEntity
[DataMember]
public int DisplayOrder { get; set; }

/// <summary>
/// Gets or sets whether to ignore charges
/// </summary>
[DataMember]
public bool IgnoreCharges { get; set; }

/// <summary>
/// Gets or sets the restricted countries
/// </summary>
/// <summary>
/// Gets or sets a value indicating whether the entity is limited/restricted to certain stores
/// </summary>
[DataMember]
public bool LimitedToStores { get; set; }

/// <summary>
/// Gets or sets the restricted countries
/// </summary>
[DataMember]
public virtual ICollection<Country> RestrictedCountries
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace SmartStore.Data.Migrations
{
using System;
using System.Data.Entity.Migrations;

public partial class ShippingMethodMultistore : DbMigration
{
public override void Up()
{
AddColumn("dbo.ShippingMethod", "LimitedToStores", c => c.Boolean(nullable: false));
}

public override void Down()
{
DropColumn("dbo.ShippingMethod", "LimitedToStores");
}
}
}

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/Libraries/SmartStore.Data/SmartStore.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,10 @@
<Compile Include="Migrations\201712290151517_AddressFormat.Designer.cs">
<DependentUpon>201712290151517_AddressFormat.cs</DependentUpon>
</Compile>
<Compile Include="Migrations\201802081135066_ShippingMethodMultistore.cs" />
<Compile Include="Migrations\201802081135066_ShippingMethodMultistore.Designer.cs">
<DependentUpon>201802081135066_ShippingMethodMultistore.cs</DependentUpon>
</Compile>
<Compile Include="ObjectContextBase.SaveChanges.cs" />
<Compile Include="Setup\Builder\ActivityLogTypeMigrator.cs" />
<Compile Include="Setup\Builder\PermissionMigrator.cs" />
Expand Down Expand Up @@ -1002,6 +1006,9 @@
<EmbeddedResource Include="Migrations\201712290151517_AddressFormat.resx">
<DependentUpon>201712290151517_AddressFormat.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Migrations\201802081135066_ShippingMethodMultistore.resx">
<DependentUpon>201802081135066_ShippingMethodMultistore.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Sql\Indexes.sql" />
<EmbeddedResource Include="Sql\StoredProcedures.sql" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ protected virtual void PrepareAuxiliaryServicesTaxingInfos(IList<OrganizedShoppi
if (shippingOption != null)
{
// use last shipping option (get from cache)
var shippingMethods = _shippingService.GetAllShippingMethods();
var shippingMethods = _shippingService.GetAllShippingMethods(null, storeId);
shippingTotal = AdjustShippingRate(shippingOption.Rate, cart, shippingOption.Name, shippingMethods, out appliedDiscount);
}
else
Expand Down
11 changes: 6 additions & 5 deletions src/Libraries/SmartStore.Services/Shipping/IShippingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ public partial interface IShippingService
ShippingMethod GetShippingMethodById(int shippingMethodId);


/// <summary>
/// Gets all shipping methods
/// </summary>
/// <summary>
/// Gets all shipping methods
/// </summary>
/// <param name="request">Shipping option request to filter out shipping methods. <c>null</c> to load all shipping methods.</param>
/// <returns>Shipping method collection</returns>
IList<ShippingMethod> GetAllShippingMethods(GetShippingOptionRequest request = null);
/// <param name="storeId">Whether to filter methods by store identifier.</param>
/// <returns>Shipping method collection</returns>
IList<ShippingMethod> GetAllShippingMethods(GetShippingOptionRequest request = null, int storeId = 0);

/// <summary>
/// Inserts a shipping method
Expand Down
42 changes: 33 additions & 9 deletions src/Libraries/SmartStore.Services/Shipping/ShippingService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SmartStore.Core.Data;
using SmartStore.Core.Domain.Catalog;
using SmartStore.Core.Domain.Common;
using SmartStore.Core.Domain.Customers;
using SmartStore.Core.Domain.Orders;
using SmartStore.Core.Domain.Shipping;
using SmartStore.Core.Domain.Stores;
using SmartStore.Core.Events;
using SmartStore.Core.Infrastructure;
using SmartStore.Core.Localization;
Expand All @@ -13,9 +17,6 @@
using SmartStore.Services.Common;
using SmartStore.Services.Configuration;
using SmartStore.Services.Orders;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SmartStore.Services.Shipping
{
Expand All @@ -25,7 +26,8 @@ public partial class ShippingService : IShippingService
private static IList<Type> _shippingMethodFilterTypes = null;

private readonly IRepository<ShippingMethod> _shippingMethodRepository;
private readonly IProductAttributeParser _productAttributeParser;
private readonly IRepository<StoreMapping> _storeMappingRepository;
private readonly IProductAttributeParser _productAttributeParser;
private readonly IProductService _productService;
private readonly ICheckoutAttributeParser _checkoutAttributeParser;
private readonly IGenericAttributeService _genericAttributeService;
Expand All @@ -39,7 +41,8 @@ public partial class ShippingService : IShippingService

public ShippingService(
IRepository<ShippingMethod> shippingMethodRepository,
IProductAttributeParser productAttributeParser,
IRepository<StoreMapping> storeMappingRepository,
IProductAttributeParser productAttributeParser,
IProductService productService,
ICheckoutAttributeParser checkoutAttributeParser,
IGenericAttributeService genericAttributeService,
Expand All @@ -52,6 +55,7 @@ public ShippingService(
ICommonServices services)
{
_shippingMethodRepository = shippingMethodRepository;
_storeMappingRepository = storeMappingRepository;
_productAttributeParser = productAttributeParser;
_productService = productService;
_checkoutAttributeParser = checkoutAttributeParser;
Expand All @@ -66,10 +70,12 @@ public ShippingService(

T = NullLocalizer.Instance;
Logger = NullLogger.Instance;
QuerySettings = DbQuerySettings.Default;
}

public Localizer T { get; set; }
public ILogger Logger { get; set; }
public DbQuerySettings QuerySettings { get; set; }

#region Shipping rate computation methods

Expand Down Expand Up @@ -160,24 +166,42 @@ public virtual ShippingMethod GetShippingMethodById(int shippingMethodId)
return _shippingMethodRepository.GetById(shippingMethodId);
}

public virtual IList<ShippingMethod> GetAllShippingMethods(GetShippingOptionRequest request = null)
public virtual IList<ShippingMethod> GetAllShippingMethods(GetShippingOptionRequest request = null, int storeId = 0)
{
var query =
from sm in _shippingMethodRepository.Table
orderby sm.DisplayOrder
select sm;

var allMethods = query.ToList();
if (!QuerySettings.IgnoreMultiStore && storeId > 0)
{
query =
from x in query
join sm in _storeMappingRepository.Table
on new { c1 = x.Id, c2 = "ShippingMethod" } equals new { c1 = sm.EntityId, c2 = sm.EntityName } into x_sm
from sm in x_sm.DefaultIfEmpty()
where !x.LimitedToStores || storeId == sm.StoreId
select x;

query =
from x in query
group x by x.Id into grp
orderby grp.Key
select grp.FirstOrDefault();
}

var allMethods = query.OrderBy(x => x.DisplayOrder).ToList();

if (request == null)
{
return allMethods;
}

IList<IShippingMethodFilter> allFilters = null;
var filterRequest = new ShippingFilterRequest { Option = request };

var activeShippingMethods = allMethods.Where(s =>
{
// shipping method filtering
// Shipping method filtering.
if (allFilters == null)
allFilters = GetAllShippingMethodFilters();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public GetShippingOptionResponse GetShippingOptions(GetShippingOptionRequest get
decimal sqThreshold = _shippingByTotalSettings.SmallQuantityThreshold;
decimal sqSurcharge = _shippingByTotalSettings.SmallQuantitySurcharge;

var shippingMethods = _shippingService.GetAllShippingMethods(getShippingOptionRequest);
var shippingMethods = _shippingService.GetAllShippingMethods(getShippingOptionRequest, storeId);
foreach (var shippingMethod in shippingMethods)
{
decimal? rate = GetRate(subTotal, shippingMethod.Id, storeId, countryId, stateProvinceId, zip);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public GetShippingOptionResponse GetShippingOptions(GetShippingOptionRequest get
return response;
}

var shippingMethods = this._shippingService.GetAllShippingMethods(getShippingOptionRequest);
var shippingMethods = this._shippingService.GetAllShippingMethods(getShippingOptionRequest, getShippingOptionRequest.StoreId);
foreach (var shippingMethod in shippingMethods)
{
var shippingOption = new ShippingOption();
Expand All @@ -82,7 +82,7 @@ public GetShippingOptionResponse GetShippingOptions(GetShippingOptionRequest get
if (getShippingOptionRequest == null)
throw new ArgumentNullException("getShippingOptionRequest");

var shippingMethods = this._shippingService.GetAllShippingMethods(getShippingOptionRequest);
var shippingMethods = _shippingService.GetAllShippingMethods(getShippingOptionRequest, getShippingOptionRequest.StoreId);

var rates = new List<decimal>();
foreach (var shippingMethod in shippingMethods)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public GetShippingOptionResponse GetShippingOptions(GetShippingOptionRequest req
}

var weight = _shippingService.GetShoppingCartTotalWeight(request.Items, _shippingByWeightSettings.IncludeWeightOfFreeShippingProducts);
var shippingMethods = _shippingService.GetAllShippingMethods(request);
var shippingMethods = _shippingService.GetAllShippingMethods(request, storeId);
currentSubTotal = _services.WorkContext.TaxDisplayType == TaxDisplayType.ExcludingTax ? subTotalExclTax : subTotalInclTax;

foreach (var shippingMethod in shippingMethods)
Expand Down
Loading

0 comments on commit 2872ff8

Please sign in to comment.