Skip to content

Commit 65a1e3e

Browse files
committed
Resolves smartstore#141 Shipping methods by customer roles
1 parent 68bc18d commit 65a1e3e

21 files changed

Lines changed: 425 additions & 270 deletions

File tree

src/Libraries/SmartStore.Core/Domain/Shipping/ShippingMethod.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public partial class ShippingMethod : BaseEntity, ILocalizedEntity
3434
[DataMember]
3535
public bool IgnoreCharges { get; set; }
3636

37+
/// <summary>
38+
/// Gets or sets identifiers of customer roles (comma separated) to be excluded in checkout
39+
/// </summary>
40+
[DataMember]
41+
public string ExcludedCustomerRoleIds { get; set; }
42+
3743
/// <summary>
3844
/// Gets or sets the restricted countries
3945
/// </summary>

src/Libraries/SmartStore.Data/Mapping/Shipping/ShippingMethodMap.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ public ShippingMethodMap()
99
{
1010
this.ToTable("ShippingMethod");
1111
this.HasKey(sm => sm.Id);
12+
1213
this.Property(sm => sm.Name).IsRequired().HasMaxLength(400);
14+
this.Property(sm => sm.ExcludedCustomerRoleIds).IsMaxLength();
1315

1416
this.HasMany(sm => sm.RestrictedCountries)
1517
.WithMany(c => c.RestrictedShippingMethods)

src/Libraries/SmartStore.Data/Migrations/201506161226375_PaymentShippingRestrictions.resx

Lines changed: 0 additions & 126 deletions
This file was deleted.

src/Libraries/SmartStore.Data/Migrations/201506161226375_PaymentShippingRestrictions.Designer.cs renamed to src/Libraries/SmartStore.Data/Migrations/201506170941137_PaymentShippingRestrictions.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Libraries/SmartStore.Data/Migrations/201506161226375_PaymentShippingRestrictions.cs renamed to src/Libraries/SmartStore.Data/Migrations/201506170941137_PaymentShippingRestrictions.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,26 @@ public partial class PaymentShippingRestrictions : DbMigration, ILocaleResources
88
{
99
public override void Up()
1010
{
11-
CreateTable(
12-
"dbo.PaymentMethod",
13-
c => new
14-
{
15-
Id = c.Int(nullable: false, identity: true),
16-
PaymentMethodSystemName = c.String(nullable: false, maxLength: 4000),
17-
ExcludedCustomerRoleIds = c.String(),
18-
ExcludedCountryIds = c.String(),
19-
ExcludedShippingMethodIds = c.String(),
20-
CountryExclusionContextId = c.Int(nullable: false),
21-
})
22-
.PrimaryKey(t => t.Id)
11+
CreateTable(
12+
"dbo.PaymentMethod",
13+
c => new
14+
{
15+
Id = c.Int(nullable: false, identity: true),
16+
PaymentMethodSystemName = c.String(nullable: false, maxLength: 4000),
17+
ExcludedCustomerRoleIds = c.String(),
18+
ExcludedCountryIds = c.String(),
19+
ExcludedShippingMethodIds = c.String(),
20+
CountryExclusionContextId = c.Int(nullable: false),
21+
})
22+
.PrimaryKey(t => t.Id)
2323
.Index(t => t.PaymentMethodSystemName);
24+
25+
AddColumn("dbo.ShippingMethod", "ExcludedCustomerRoleIds", c => c.String());
2426
}
2527

2628
public override void Down()
2729
{
30+
DropColumn("dbo.ShippingMethod", "ExcludedCustomerRoleIds");
2831
DropTable("dbo.PaymentMethod");
2932
}
3033

@@ -59,6 +62,10 @@ public void MigrateLocaleResources(LocaleResourcesBuilder builder)
5962
builder.AddOrUpdate("Enums.SmartStore.Core.Domain.Payments.CountryExclusionContextType.ShippingAddress",
6063
"Shipping address",
6164
"Versandadresse");
65+
66+
builder.AddOrUpdate("Admin.Configuration.Shipping.Methods.RestrictionNote",
67+
"Select customer roles for which you do <u>not</u> want to offer this shipping method.",
68+
"Wählen Sie Kundengruppen, bei denen sie diese Versandart <u>nicht</u> anbieten möchten.");
6269
}
6370
}
6471
}

src/Libraries/SmartStore.Data/Migrations/201506170941137_PaymentShippingRestrictions.resx

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

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,9 @@
273273
<Compile Include="Migrations\201505272236124_V222.Designer.cs">
274274
<DependentUpon>201505272236124_V222.cs</DependentUpon>
275275
</Compile>
276-
<Compile Include="Migrations\201506161226375_PaymentShippingRestrictions.cs" />
277-
<Compile Include="Migrations\201506161226375_PaymentShippingRestrictions.Designer.cs">
278-
<DependentUpon>201506161226375_PaymentShippingRestrictions.cs</DependentUpon>
276+
<Compile Include="Migrations\201506170941137_PaymentShippingRestrictions.cs" />
277+
<Compile Include="Migrations\201506170941137_PaymentShippingRestrictions.Designer.cs">
278+
<DependentUpon>201506170941137_PaymentShippingRestrictions.cs</DependentUpon>
279279
</Compile>
280280
<Compile Include="Setup\Builder\SettingsBuilder.cs" />
281281
<Compile Include="Setup\Builder\SettingsMigrator.cs" />
@@ -534,8 +534,8 @@
534534
<EmbeddedResource Include="Migrations\201505272236124_V222.resx">
535535
<DependentUpon>201505272236124_V222.cs</DependentUpon>
536536
</EmbeddedResource>
537-
<EmbeddedResource Include="Migrations\201506161226375_PaymentShippingRestrictions.resx">
538-
<DependentUpon>201506161226375_PaymentShippingRestrictions.cs</DependentUpon>
537+
<EmbeddedResource Include="Migrations\201506170941137_PaymentShippingRestrictions.resx">
538+
<DependentUpon>201506170941137_PaymentShippingRestrictions.cs</DependentUpon>
539539
</EmbeddedResource>
540540
<EmbeddedResource Include="Sql\Indexes.sql" />
541541
<EmbeddedResource Include="Sql\StoredProcedures.sql" />

src/Libraries/SmartStore.Services/Shipping/IShippingService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using SmartStore.Core.Domain.Common;
3+
using SmartStore.Core.Domain.Customers;
34
using SmartStore.Core.Domain.Orders;
45
using SmartStore.Core.Domain.Shipping;
56
using SmartStore.Core.Plugins;
@@ -53,8 +54,9 @@ public partial interface IShippingService
5354
/// Gets all shipping methods
5455
/// </summary>
5556
/// <param name="filterByCountryId">The country indentifier to filter by</param>
57+
/// <param name="customer">Filter methods by roles of a customer</param>
5658
/// <returns>Shipping method collection</returns>
57-
IList<ShippingMethod> GetAllShippingMethods(int? filterByCountryId = null);
59+
IList<ShippingMethod> GetAllShippingMethods(int? filterByCountryId = null, Customer customer = null);
5860

5961
/// <summary>
6062
/// Inserts a shipping method

src/Libraries/SmartStore.Services/Shipping/ShippingService.cs

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public ShippingService(ICacheManager cacheManager,
103103
public virtual IEnumerable<Provider<IShippingRateComputationMethod>> LoadActiveShippingRateComputationMethods(int storeId = 0)
104104
{
105105
var allMethods = LoadAllShippingRateComputationMethods(storeId);
106+
106107
var activeMethods = allMethods
107108
.Where(p => p.Value.IsActive && _shippingSettings.ActiveShippingRateComputationMethodSystemNames.Contains(p.Metadata.SystemName, StringComparer.InvariantCultureIgnoreCase));
108109

@@ -114,6 +115,7 @@ public virtual IEnumerable<Provider<IShippingRateComputationMethod>> LoadActiveS
114115
_shippingSettings.ActiveShippingRateComputationMethodSystemNames.Clear();
115116
_shippingSettings.ActiveShippingRateComputationMethodSystemNames.Add(fallbackMethod.Metadata.SystemName);
116117
_settingService.SaveSetting(_shippingSettings);
118+
117119
return new Provider<IShippingRateComputationMethod>[] { fallbackMethod };
118120
}
119121
else
@@ -182,32 +184,57 @@ public virtual ShippingMethod GetShippingMethodById(int shippingMethodId)
182184
/// Gets all shipping methods
183185
/// </summary>
184186
/// <param name="filterByCountryId">The country indentifier to filter by</param>
187+
/// <param name="customer">Filter methods by roles of a customer</param>
185188
/// <returns>Shipping method collection</returns>
186-
public virtual IList<ShippingMethod> GetAllShippingMethods(int? filterByCountryId = null)
189+
public virtual IList<ShippingMethod> GetAllShippingMethods(int? filterByCountryId = null, Customer customer = null)
187190
{
191+
IQueryable<ShippingMethod> query = null;
192+
188193
if (filterByCountryId.HasValue && filterByCountryId.Value > 0)
189194
{
190-
var query1 = from sm in _shippingMethodRepository.Table
191-
where
192-
sm.RestrictedCountries.Select(c => c.Id).Contains(filterByCountryId.Value)
193-
select sm.Id;
194-
195-
var query2 = from sm in _shippingMethodRepository.Table
196-
where !query1.Contains(sm.Id)
197-
orderby sm.DisplayOrder
198-
select sm;
199-
200-
var shippingMethods = query2.ToList();
201-
return shippingMethods;
195+
var query1 =
196+
from sm in _shippingMethodRepository.Table
197+
where sm.RestrictedCountries.Select(c => c.Id).Contains(filterByCountryId.Value)
198+
select sm.Id;
199+
200+
query =
201+
from sm in _shippingMethodRepository.Table
202+
where !query1.Contains(sm.Id)
203+
orderby sm.DisplayOrder
204+
select sm;
202205
}
203206
else
204207
{
205-
var query = from sm in _shippingMethodRepository.Table
206-
orderby sm.DisplayOrder
207-
select sm;
208-
var shippingMethods = query.ToList();
209-
return shippingMethods;
208+
query =
209+
from sm in _shippingMethodRepository.Table
210+
orderby sm.DisplayOrder
211+
select sm;
210212
}
213+
214+
var shippingMethods = query.ToList();
215+
216+
if (customer != null)
217+
{
218+
List<int> customerRoleIds = null;
219+
220+
for (int i = shippingMethods.Count - 1; i >= 0; --i)
221+
{
222+
var method = shippingMethods[i];
223+
224+
// method restricted by customer role id?
225+
var excludedRoleIds = method.ExcludedCustomerRoleIds.ToIntArray();
226+
if (excludedRoleIds.Any())
227+
{
228+
if (customerRoleIds == null)
229+
customerRoleIds = customer.CustomerRoles.Where(r => r.Active).Select(r => r.Id).ToList();
230+
231+
if (customerRoleIds != null && !customerRoleIds.Except(excludedRoleIds).Any())
232+
shippingMethods.Remove(method);
233+
}
234+
}
235+
}
236+
237+
return shippingMethods;
211238
}
212239

213240
/// <summary>
@@ -357,7 +384,8 @@ public virtual GetShippingOptionRequest CreateShippingOptionRequest(IList<Organi
357384
/// <param name="allowedShippingRateComputationMethodSystemName">Filter by shipping rate computation method identifier; null to load shipping options of all shipping rate computation methods</param>
358385
/// <param name="storeId">Load records allows only in specified store; pass 0 to load all records</param>
359386
/// <returns>Shipping options</returns>
360-
public virtual GetShippingOptionResponse GetShippingOptions(IList<OrganizedShoppingCartItem> cart, Address shippingAddress, string allowedShippingRateComputationMethodSystemName = "", int storeId = 0)
387+
public virtual GetShippingOptionResponse GetShippingOptions(IList<OrganizedShoppingCartItem> cart, Address shippingAddress,
388+
string allowedShippingRateComputationMethodSystemName = "", int storeId = 0)
361389
{
362390
if (cart == null)
363391
throw new ArgumentNullException("cart");

src/Plugins/SmartStore.Shipping/Description.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Description: Provides shipping methods for fixed rate shipping and computation based on weight.
33
SystemName: SmartStore.Shipping
44
Group: Shipping
5-
Version: 2.2.0
5+
Version: 2.2.0.1
66
MinAppVersion: 2.2.0
77
DisplayOrder: 1
88
FileName: SmartStore.Shipping.dll

0 commit comments

Comments
 (0)