Skip to content

Commit

Permalink
Refactored AddToCart.
Browse files Browse the repository at this point in the history
Fixed "Adding a relationship with an entity which is in the Deleted state is not allowed" when adding bundles to cart.
Fixed price calculation of multiple bundles issue.
Fixed auto add required products for bundle items.
  • Loading branch information
mgesing committed Feb 19, 2015
1 parent dbc35fc commit 018c5be
Show file tree
Hide file tree
Showing 12 changed files with 327 additions and 222 deletions.
6 changes: 5 additions & 1 deletion changelog.md
@@ -1,4 +1,4 @@
# Release Notes
# Release Notes

## SmartStore.NET 2.2

Expand All @@ -17,6 +17,7 @@
* Payone: CC-Check via client API, not via Server API (requires PCI certification)
* #189 Allow deletion of multiple reviews
* #622 UI: Redesign table in Sales > Orders > Order > Tab Products
* #625 Bundles can be ordered if an attribute combination of a bundle item is not available

### Bugfixes
* Amazon payments: Declined authorization IPN did not void the payment status
Expand All @@ -29,6 +30,9 @@
* Print order as pdf redirected to login although the admin already was logged in
* #621 PDF Order: does not take overridden attribute combination price into account (in order line)
* Hide additional shipping surcharge when display prices permission is not granted
* Fixed "Adding a relationship with an entity which is in the Deleted state is not allowed" when adding bundles to cart
* Fixed price calculation of multiple bundles issue
* Fixed auto add required products for bundle items


## SmartStore.NET 2.1.1
Expand Down
Expand Up @@ -556,6 +556,11 @@ public virtual decimal GetUnitPrice(OrganizedShoppingCartItem shoppingCartItem,
{
if (shoppingCartItem.ChildItems != null)
{
foreach (var bundleItem in shoppingCartItem.ChildItems)
{
bundleItem.Item.Product.MergeWithCombination(bundleItem.Item.AttributesXml);
}

var bundleItems = shoppingCartItem.ChildItems.Where(x => x.BundleItemData.IsValid()).Select(x => x.BundleItemData).ToList();

finalPrice = GetFinalPrice(product, bundleItems, customer, decimal.Zero, includeDiscounts, shoppingCartItem.Item.Quantity);
Expand Down
42 changes: 42 additions & 0 deletions src/Libraries/SmartStore.Services/Orders/AddToCartContext.cs
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using SmartStore.Core.Domain.Catalog;
using SmartStore.Core.Domain.Customers;
using SmartStore.Core.Domain.Orders;

namespace SmartStore.Services.Orders
{
public class AddToCartContext
{
public AddToCartContext()
{
Warnings = new List<string>();
CustomerEnteredPrice = decimal.Zero;
ChildItems = new List<ShoppingCartItem>();
}

public List<string> Warnings { get; set; }

public ShoppingCartItem Item { get; set; }
public List<ShoppingCartItem> ChildItems { get; set; }
public ProductBundleItem BundleItem { get; set; }

public Customer Customer { get; set; }
public Product Product { get; set; }
public ShoppingCartType CartType { get; set; }
public NameValueCollection AttributeForm { get; set; }
public string Attributes { get; set; }
public decimal CustomerEnteredPrice { get; set; }
public int Quantity { get; set; }
public bool AddRequiredProducts { get; set; }
public int? StoreId { get; set; }

public int BundleItemId
{
get
{
return (BundleItem == null ? 0 : BundleItem.Id);
}
}
}
}
56 changes: 23 additions & 33 deletions src/Libraries/SmartStore.Services/Orders/IShoppingCartService.cs
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using SmartStore.Core.Domain.Catalog;
using SmartStore.Core.Domain.Customers;
using SmartStore.Core.Domain.Orders;
Expand Down Expand Up @@ -143,42 +142,33 @@ public partial interface IShoppingCartService
string selectedAttributes = "",
decimal customerEnteredPrice = decimal.Zero);


/// <summary>
/// Add a product to shopping cart
/// </summary>
/// <param name="customer">Customer</param>
/// <param name="product">Product</param>
/// <param name="shoppingCartType">Shopping cart type</param>
/// <summary>
/// Add product to cart
/// </summary>
/// <param name="customer">The customer</param>
/// <param name="product">The product</param>
/// <param name="cartType">Cart type</param>
/// <param name="storeId">Store identifier</param>
/// <param name="selectedAttributes">Selected attributes</param>
/// <param name="customerEnteredPrice">The price enter by a customer</param>
/// <param name="quantity">Quantity</param>
/// <param name="automaticallyAddRequiredProductsIfEnabled">Automatically add required products if enabled</param>
/// <param name="shoppingCartItemId">Identifier fo the new shopping cart item</param>
/// <param name="parentItemId">Identifier of the parent shopping cart item</param>
/// <param name="bundleItem">Product bundle item</param>
/// <returns>Warnings</returns>
IList<string> AddToCart(Customer customer, Product product,
ShoppingCartType shoppingCartType, int storeId, string selectedAttributes,
decimal customerEnteredPrice, int quantity, bool automaticallyAddRequiredProductsIfEnabled,
out int shoppingCartItemId, int? parentItemId = null, ProductBundleItem bundleItem = null);
/// <param name="selectedAttributes">Selected attributes</param>
/// <param name="customerEnteredPrice">Price entered by customer</param>
/// <param name="quantity">Quantity</param>
/// <param name="automaticallyAddRequiredProductsIfEnabled">Whether to add required products</param>
/// <param name="ctx">Add to cart context</param>
/// <returns>List with warnings</returns>
List<string> AddToCart(Customer customer, Product product, ShoppingCartType cartType, int storeId, string selectedAttributes,
decimal customerEnteredPrice, int quantity, bool automaticallyAddRequiredProductsIfEnabled, AddToCartContext ctx = null);

/// <summary>
/// Adds a product to the shopping cart and also adds bundle items if the product is a bundle.
/// Add product to cart
/// </summary>
/// <param name="warnings">List with warnings</param>
/// <param name="product">Product</param>
/// <param name="form">Collection with selected attribute data</param>
/// <param name="cartType">Shopping cart type</param>
/// <param name="customerEnteredPrice">The price enter by a customer</param>
/// <param name="quantity">Quantity</param>
/// <param name="addRequiredProducts">Automatically add required products if enabled</param>
/// <param name="parentCartItemId">Parent cart item if it is a bundle item</param>
/// <param name="bundleItem">Bundle item object if it is a bundle item</param>
/// <returns>Identifier of inserted (parent) shopping cart item</returns>
int AddToCart(List<string> warnings, Product product, NameValueCollection form, ShoppingCartType cartType, decimal customerEnteredPrice,
int quantity, bool addRequiredProducts, int? parentCartItemId = null, ProductBundleItem bundleItem = null);
/// <param name="ctx">Add to cart context</param>
void AddToCart(AddToCartContext ctx);

/// <summary>
/// Stores the shopping card items in the database
/// </summary>
/// <param name="ctx">Add to cart context</param>
void AddToCartStore(AddToCartContext ctx);

/// <summary>
/// Updates the shopping cart item
Expand Down
28 changes: 13 additions & 15 deletions src/Libraries/SmartStore.Services/Orders/OrderProcessingService.cs
Expand Up @@ -4,7 +4,6 @@
using System.Linq;
using System.Text;
using SmartStore.Core;
using SmartStore.Core.Html;
using SmartStore.Core.Domain.Catalog;
using SmartStore.Core.Domain.Common;
using SmartStore.Core.Domain.Customers;
Expand All @@ -16,22 +15,21 @@
using SmartStore.Core.Domain.Payments;
using SmartStore.Core.Domain.Shipping;
using SmartStore.Core.Domain.Tax;
using SmartStore.Core.Events;
using SmartStore.Core.Logging;
using SmartStore.Core.Plugins;
using SmartStore.Services.Affiliates;
using SmartStore.Services.Catalog;
using SmartStore.Services.Common;
using SmartStore.Services.Customers;
using SmartStore.Services.Directory;
using SmartStore.Services.Discounts;
using SmartStore.Core.Events;
using SmartStore.Services.Localization;
using SmartStore.Core.Logging;
using SmartStore.Services.Messages;
using SmartStore.Services.Payments;
using SmartStore.Services.Security;
using SmartStore.Services.Shipping;
using SmartStore.Services.Tax;
using SmartStore.Services.Seo;
using SmartStore.Core.Plugins;

namespace SmartStore.Services.Orders
{
Expand Down Expand Up @@ -2655,28 +2653,28 @@ public virtual void ReOrder(Order order)
if (order == null)
throw new ArgumentNullException("order");

int parentItemId, childItemId;

foreach (var orderItem in order.OrderItems)
{
bool isBundle = (orderItem.Product.ProductType == ProductType.BundledProduct);

var warnings =_shoppingCartService.AddToCart(orderItem.Order.Customer, orderItem.Product, ShoppingCartType.ShoppingCart, orderItem.Order.StoreId,
orderItem.AttributesXml, isBundle ? decimal.Zero : orderItem.UnitPriceExclTax, orderItem.Quantity, false, out parentItemId);
var addToCartContext = new AddToCartContext();

addToCartContext.Warnings = _shoppingCartService.AddToCart(order.Customer, orderItem.Product, ShoppingCartType.ShoppingCart, order.StoreId,
orderItem.AttributesXml, isBundle ? decimal.Zero : orderItem.UnitPriceExclTax, orderItem.Quantity, false, addToCartContext);

if (isBundle && orderItem.BundleData.HasValue() && warnings.Count <= 0)
if (isBundle && orderItem.BundleData.HasValue() && addToCartContext.Warnings.Count == 0)
{
foreach (var bundleData in orderItem.GetBundleData())
{
var bundleItem = _productService.GetBundleItemById(bundleData.BundleItemId);
addToCartContext.BundleItem = bundleItem;

warnings =_shoppingCartService.AddToCart(orderItem.Order.Customer, bundleItem.Product, ShoppingCartType.ShoppingCart,
orderItem.Order.StoreId, bundleData.AttributesXml, decimal.Zero, bundleData.Quantity, false, out childItemId, parentItemId, bundleItem);

if (warnings.Count > 0)
_shoppingCartService.DeleteShoppingCartItem(parentItemId);
addToCartContext.Warnings = _shoppingCartService.AddToCart(order.Customer, bundleItem.Product, ShoppingCartType.ShoppingCart, order.StoreId,
bundleData.AttributesXml, decimal.Zero, bundleData.Quantity, false, addToCartContext);
}
}

_shoppingCartService.AddToCartStore(addToCartContext);
}
}

Expand Down

0 comments on commit 018c5be

Please sign in to comment.