Skip to content

Commit

Permalink
New option whether to include the weight of free shipping products in…
Browse files Browse the repository at this point in the history
… shipping by weight calculation. Resolves #922.
  • Loading branch information
mgesing committed Aug 9, 2016
1 parent ec5e463 commit 7c650b0
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 28 deletions.
1 change: 1 addition & 0 deletions changelog.md
Expand Up @@ -15,6 +15,7 @@
* Added projection to control the export of individually visible associated products
* #1002 Web API: Add support for addresses and customer roles navigation property of customer entity
* #966 Implement new tax calculation logic for shipping and payment fees
* #922 New option whether to include the weight of free shipping products in shipping by weight calculation

### Bugfixes
* Currency wasn't displayed at shipping estimation
Expand Down
14 changes: 7 additions & 7 deletions src/Libraries/SmartStore.Services/Shipping/IShippingService.cs
@@ -1,16 +1,15 @@
using System.Collections.Generic;
using SmartStore.Core.Domain.Common;
using SmartStore.Core.Domain.Customers;
using SmartStore.Core.Domain.Orders;
using SmartStore.Core.Domain.Shipping;
using SmartStore.Core.Plugins;

namespace SmartStore.Services.Shipping
{
/// <summary>
/// Shipping service interface
/// </summary>
public partial interface IShippingService
/// <summary>
/// Shipping service interface
/// </summary>
public partial interface IShippingService
{
/// <summary>
/// Load active shipping rate computation methods
Expand Down Expand Up @@ -77,7 +76,6 @@ public partial interface IShippingService
/// <returns>Shopping cart item weight</returns>
decimal GetShoppingCartItemWeight(OrganizedShoppingCartItem shoppingCartItem);


/// <summary>
/// Gets shopping cart item total weight
/// </summary>
Expand All @@ -89,8 +87,10 @@ public partial interface IShippingService
/// Gets shopping cart weight
/// </summary>
/// <param name="cart">Cart</param>
/// <param name="includeFreeShippingProducts">Whether to include free shipping products</param>
/// <returns>Shopping cart weight</returns>
decimal GetShoppingCartTotalWeight(IList<OrganizedShoppingCartItem> cart);
decimal GetShoppingCartTotalWeight(IList<OrganizedShoppingCartItem> cart, bool includeFreeShippingProducts = true);


/// <summary>
/// Create shipment package from shopping cart
Expand Down
38 changes: 24 additions & 14 deletions src/Libraries/SmartStore.Services/Shipping/ShippingService.cs
Expand Up @@ -263,11 +263,11 @@ public virtual decimal GetShoppingCartItemWeight(OrganizedShoppingCartItem shopp
if (shoppingCartItem == null)
throw new ArgumentNullException("shoppingCartItem");

decimal weight = decimal.Zero;
var weight = decimal.Zero;

if (shoppingCartItem.Item.Product != null)
{
decimal attributesTotalWeight = decimal.Zero;
var attributesTotalWeight = decimal.Zero;

if (!String.IsNullOrEmpty(shoppingCartItem.Item.AttributesXml))
{
Expand Down Expand Up @@ -303,23 +303,31 @@ public virtual decimal GetShoppingCartItemTotalWeight(OrganizedShoppingCartItem
if (shoppingCartItem == null)
throw new ArgumentNullException("shoppingCartItem");

decimal totalWeight = GetShoppingCartItemWeight(shoppingCartItem) * shoppingCartItem.Item.Quantity;
var totalWeight = GetShoppingCartItemWeight(shoppingCartItem) * shoppingCartItem.Item.Quantity;
return totalWeight;
}

/// <summary>
/// Gets shopping cart weight
/// </summary>
/// <param name="cart">Cart</param>
/// <returns>Shopping cart weight</returns>
public virtual decimal GetShoppingCartTotalWeight(IList<OrganizedShoppingCartItem> cart)
public virtual decimal GetShoppingCartTotalWeight(IList<OrganizedShoppingCartItem> cart, bool includeFreeShippingProducts = true)
{
Customer customer = cart.GetCustomer();
var totalWeight = decimal.Zero;
var customer = cart.GetCustomer();

decimal totalWeight = decimal.Zero;
// shopping cart items
foreach (var shoppingCartItem in cart)
totalWeight += GetShoppingCartItemTotalWeight(shoppingCartItem);
// shopping cart items
foreach (var cartItem in cart)
{
var product = cartItem.Item.Product;
if (product != null)
{
if (!includeFreeShippingProducts && product.IsFreeShipping)
{
// skip product
}
else
{
totalWeight += GetShoppingCartItemTotalWeight(cartItem);
}
}
}

// checkout attributes
if (customer != null)
Expand All @@ -329,7 +337,9 @@ public virtual decimal GetShoppingCartTotalWeight(IList<OrganizedShoppingCartIte
{
var caValues = _checkoutAttributeParser.ParseCheckoutAttributeValues(checkoutAttributesXml);
foreach (var caValue in caValues)
{
totalWeight += caValue.WeightAdjustment;
}
}
}

Expand Down
Expand Up @@ -148,7 +148,8 @@ public GetShippingOptionResponse GetShippingOptions(GetShippingOptionRequest req
continue;
subTotal += _priceCalculationService.GetSubTotal(shoppingCartItem, true);
}
decimal weight = _shippingService.GetShoppingCartTotalWeight(request.Items);

var weight = _shippingService.GetShoppingCartTotalWeight(request.Items, _shippingByWeightSettings.IncludeWeightOfFreeShippingProducts);

var shippingMethods = _shippingService.GetAllShippingMethods(request);
foreach (var shippingMethod in shippingMethods)
Expand Down
Expand Up @@ -79,6 +79,7 @@ public ActionResult Configure()

model.LimitMethodsToCreated = _shippingByWeightSettings.LimitMethodsToCreated;
model.CalculatePerWeightUnit = _shippingByWeightSettings.CalculatePerWeightUnit;
model.IncludeWeightOfFreeShippingProducts = _shippingByWeightSettings.IncludeWeightOfFreeShippingProducts;
model.PrimaryStoreCurrencyCode = _services.StoreContext.CurrentStore.PrimaryStoreCurrency.CurrencyCode;
model.BaseWeightIn = _measureService.GetMeasureWeightById(_measureSettings.BaseWeightId).Name;
model.GridPageSize = _adminAreaSettings.GridPageSize;
Expand Down Expand Up @@ -166,6 +167,8 @@ public ActionResult SaveGeneralSettings(ShippingByWeightListModel model)
//save settings
_shippingByWeightSettings.LimitMethodsToCreated = model.LimitMethodsToCreated;
_shippingByWeightSettings.CalculatePerWeightUnit = model.CalculatePerWeightUnit;
_shippingByWeightSettings.IncludeWeightOfFreeShippingProducts = model.IncludeWeightOfFreeShippingProducts;

_services.Settings.SaveSetting(_shippingByWeightSettings);

return Configure();
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/SmartStore.ShippingByWeight/Description.txt
@@ -1,7 +1,7 @@
FriendlyName: Shipping by weight
SystemName: SmartStore.ShippingByWeight
Group: Shipping
Version: 2.6.0
Version: 2.6.0.1
MinAppVersion: 2.5.0
DisplayOrder: 1
FileName: SmartStore.ShippingByWeight.dll
Expand Down
Expand Up @@ -63,6 +63,12 @@
<LocaleResource Name="Fields.CalculatePerWeightUnit.Hint">
<Value>Legt fest, ob die Gebühr mit dem Gesamt-Warengewicht multipliziert werden soll. Diese Option wird ignoriert, wenn eine prozentuale Gebühr erhoben wird.</Value>
</LocaleResource>
<LocaleResource Name="Fields.IncludeWeightOfFreeShippingProducts">
<Value>Gewicht versandkostenbefreiter Produkte berücksichtigen</Value>
</LocaleResource>
<LocaleResource Name="Fields.IncludeWeightOfFreeShippingProducts.Hint">
<Value>Legt fest, ob das Gewicht von versandkostenbefreiten Produkten bei der Berechnung berücksichtigt werden soll.</Value>
</LocaleResource>
<LocaleResource Name="Fields.Store">
<Value>Shop</Value>
</LocaleResource>
Expand Down
Expand Up @@ -61,6 +61,12 @@
</LocaleResource>
<LocaleResource Name="Fields.CalculatePerWeightUnit.Hint">
<Value>If you check this option, then rates are multiplied per weight unit (lb, kg, etc). This option is used for the fixed rates (without percents).</Value>
</LocaleResource>
<LocaleResource Name="Fields.IncludeWeightOfFreeShippingProducts">
<Value>Include weight of free shipping products</Value>
</LocaleResource>
<LocaleResource Name="Fields.IncludeWeightOfFreeShippingProducts.Hint">
<Value>Specifies whether to include the weight of free shipping products in the calculation.</Value>
</LocaleResource>
<LocaleResource Name="Fields.Store">
<Value>Store</Value>
Expand Down
Expand Up @@ -44,7 +44,10 @@ public ShippingByWeightListModel()
[SmartResourceDisplayName("Plugins.Shipping.ByWeight.Fields.CalculatePerWeightUnit")]
public bool CalculatePerWeightUnit { get; set; }

public string PrimaryStoreCurrencyCode { get; set; }
[SmartResourceDisplayName("Plugins.Shipping.ByWeight.Fields.IncludeWeightOfFreeShippingProducts")]
public bool IncludeWeightOfFreeShippingProducts { get; set; }

public string PrimaryStoreCurrencyCode { get; set; }
public string BaseWeightIn { get; set; }

public int GridPageSize { get; set; }
Expand Down
@@ -1,12 +1,21 @@

using SmartStore.Core.Configuration;
using SmartStore.Core.Configuration;

namespace SmartStore.ShippingByWeight
{
public class ShippingByWeightSettings : ISettings
{
public bool LimitMethodsToCreated { get; set; }
public ShippingByWeightSettings()
{
IncludeWeightOfFreeShippingProducts = true;
}

public bool LimitMethodsToCreated { get; set; }

public bool CalculatePerWeightUnit { get; set; }
}

/// <summary>
/// Whether to include the weight of free shipping products in shipping calculation
/// </summary>
public bool IncludeWeightOfFreeShippingProducts { get; set; }
}
}
Expand Up @@ -229,6 +229,15 @@
@Html.ValidationMessageFor(model => model.LimitMethodsToCreated)
</td>
</tr>
<tr>
<td class="adminTitle">
@Html.SmartLabelFor(model => model.IncludeWeightOfFreeShippingProducts)
</td>
<td class="adminData">
@Html.EditorFor(model => model.IncludeWeightOfFreeShippingProducts)
@Html.ValidationMessageFor(model => model.IncludeWeightOfFreeShippingProducts)
</td>
</tr>
<tr>
<td class="adminTitle">
&nbsp;
Expand Down

0 comments on commit 7c650b0

Please sign in to comment.