本文整理汇总了C#中Nop.Web.Models.ShoppingCart.WishlistModel类的典型用法代码示例。如果您正苦于以下问题:C# WishlistModel类的具体用法?C# WishlistModel怎么用?C# WishlistModel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WishlistModel类属于Nop.Web.Models.ShoppingCart命名空间,在下文中一共展示了WishlistModel类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddItemsToCartFromWishlist
public ActionResult AddItemsToCartFromWishlist(Guid? customerGuid, FormCollection form)
{
if (!_permissionService.Authorize(StandardPermissionProvider.EnableShoppingCart))
return RedirectToRoute("HomePage");
if (!_permissionService.Authorize(StandardPermissionProvider.EnableWishlist))
return RedirectToRoute("HomePage");
var pageCustomer = customerGuid.HasValue
? _customerService.GetCustomerByGuid(customerGuid.Value)
: _workContext.CurrentCustomer;
if (pageCustomer == null)
return RedirectToRoute("HomePage");
var pageCart = pageCustomer.ShoppingCartItems
.Where(sci => sci.ShoppingCartType == ShoppingCartType.Wishlist)
.LimitPerStore(_storeContext.CurrentStore.Id)
.ToList();
var allWarnings = new List<string>();
var numberOfAddedItems = 0;
var allIdsToAdd = form["addtocart"] != null ? form["addtocart"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList() : new List<int>();
foreach (var sci in pageCart)
{
if (allIdsToAdd.Contains(sci.Id))
{
var warnings = _shoppingCartService.AddToCart(_workContext.CurrentCustomer,
sci.Product, ShoppingCartType.ShoppingCart,
_storeContext.CurrentStore.Id,
sci.AttributesXml, sci.CustomerEnteredPrice, sci.Quantity, true);
if (warnings.Count == 0)
numberOfAddedItems++;
if (_shoppingCartSettings.MoveItemsFromWishlistToCart && //settings enabled
!customerGuid.HasValue && //own wishlist
warnings.Count == 0) //no warnings ( already in the cart)
{
//let's remove the item from wishlist
_shoppingCartService.DeleteShoppingCartItem(sci);
}
allWarnings.AddRange(warnings);
}
}
if (numberOfAddedItems > 0)
{
//redirect to the shopping cart page
return RedirectToRoute("ShoppingCart");
}
else
{
//no items added. redisplay the wishlist page
var cart = pageCustomer.ShoppingCartItems
.Where(sci => sci.ShoppingCartType == ShoppingCartType.Wishlist)
.LimitPerStore(_storeContext.CurrentStore.Id)
.ToList();
var model = new WishlistModel();
PrepareWishlistModel(model, cart, !customerGuid.HasValue);
return View(model);
}
}
示例2: PrepareWishlistModel
protected virtual void PrepareWishlistModel(WishlistModel model,
IList<ShoppingCartItem> cart, bool isEditable = true)
{
if (cart == null)
throw new ArgumentNullException("cart");
if (model == null)
throw new ArgumentNullException("model");
model.EmailWishlistEnabled = _shoppingCartSettings.EmailWishlistEnabled;
model.IsEditable = isEditable;
model.DisplayAddToCart = _permissionService.Authorize(StandardPermissionProvider.EnableShoppingCart);
if (cart.Count == 0)
return;
#region Simple properties
var customer = cart.FirstOrDefault().Customer;
model.CustomerGuid = customer.CustomerGuid;
model.CustomerFullname = customer.GetFullName();
model.ShowProductImages = _shoppingCartSettings.ShowProductImagesOnShoppingCart;
model.ShowSku = _catalogSettings.ShowProductSku;
//cart warnings
var cartWarnings = _shoppingCartService.GetShoppingCartWarnings(cart, "", false);
foreach (var warning in cartWarnings)
model.Warnings.Add(warning);
#endregion
#region Cart items
foreach (var sci in cart)
{
var cartItemModel = new WishlistModel.ShoppingCartItemModel()
{
Id = sci.Id,
Sku = sci.Product.FormatSku(sci.AttributesXml, _productAttributeParser),
ProductId = sci.Product.Id,
ProductName = sci.Product.GetLocalized(x => x.Name),
ProductSeName = sci.Product.GetSeName(),
Quantity = sci.Quantity,
AttributeInfo = _productAttributeFormatter.FormatAttributes(sci.Product, sci.AttributesXml),
};
//allowed quantities
var allowedQuantities = sci.Product.ParseAllowedQuatities();
foreach (var qty in allowedQuantities)
{
cartItemModel.AllowedQuantities.Add(new SelectListItem()
{
Text = qty.ToString(),
Value = qty.ToString(),
Selected = sci.Quantity == qty
});
}
//recurring info
if (sci.Product.IsRecurring)
cartItemModel.RecurringInfo = string.Format(_localizationService.GetResource("ShoppingCart.RecurringPeriod"), sci.Product.RecurringCycleLength, sci.Product.RecurringCyclePeriod.GetLocalizedEnum(_localizationService, _workContext));
//unit prices
if (sci.Product.CallForPrice)
{
cartItemModel.UnitPrice = _localizationService.GetResource("Products.CallForPrice");
}
else
{
decimal taxRate = decimal.Zero;
decimal shoppingCartUnitPriceWithDiscountBase = _taxService.GetProductPrice(sci.Product, _priceCalculationService.GetUnitPrice(sci, true), out taxRate);
decimal shoppingCartUnitPriceWithDiscount = _currencyService.ConvertFromPrimaryStoreCurrency(shoppingCartUnitPriceWithDiscountBase, _workContext.WorkingCurrency);
cartItemModel.UnitPrice = _priceFormatter.FormatPrice(shoppingCartUnitPriceWithDiscount);
}
//subtotal, discount
if (sci.Product.CallForPrice)
{
cartItemModel.SubTotal = _localizationService.GetResource("Products.CallForPrice");
}
else
{
//sub total
decimal taxRate = decimal.Zero;
decimal shoppingCartItemSubTotalWithDiscountBase = _taxService.GetProductPrice(sci.Product, _priceCalculationService.GetSubTotal(sci, true), out taxRate);
decimal shoppingCartItemSubTotalWithDiscount = _currencyService.ConvertFromPrimaryStoreCurrency(shoppingCartItemSubTotalWithDiscountBase, _workContext.WorkingCurrency);
cartItemModel.SubTotal = _priceFormatter.FormatPrice(shoppingCartItemSubTotalWithDiscount);
//display an applied discount amount
decimal shoppingCartItemSubTotalWithoutDiscountBase = _taxService.GetProductPrice(sci.Product, _priceCalculationService.GetSubTotal(sci, false), out taxRate);
decimal shoppingCartItemDiscountBase = shoppingCartItemSubTotalWithoutDiscountBase - shoppingCartItemSubTotalWithDiscountBase;
if (shoppingCartItemDiscountBase > decimal.Zero)
{
decimal shoppingCartItemDiscount = _currencyService.ConvertFromPrimaryStoreCurrency(shoppingCartItemDiscountBase, _workContext.WorkingCurrency);
cartItemModel.Discount = _priceFormatter.FormatPrice(shoppingCartItemDiscount);
}
}
//picture
if (_shoppingCartSettings.ShowProductImagesOnShoppingCart)
{
//.........这里部分代码省略.........
示例3: UpdateWishlist
public ActionResult UpdateWishlist(FormCollection form)
{
if (!_permissionService.Authorize(StandardPermissionProvider.EnableWishlist))
return RedirectToRoute("HomePage");
var cart = _workContext.CurrentCustomer.ShoppingCartItems
.Where(sci => sci.ShoppingCartType == ShoppingCartType.Wishlist)
.LimitPerStore(_storeContext.CurrentStore.Id)
.ToList();
var allIdsToRemove = form["removefromcart"] != null ? form["removefromcart"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList() : new List<int>();
//current warnings <cart item identifier, warnings>
var innerWarnings = new Dictionary<int, IList<string>>();
foreach (var sci in cart)
{
bool remove = allIdsToRemove.Contains(sci.Id);
if (remove)
_shoppingCartService.DeleteShoppingCartItem(sci);
else
{
foreach (string formKey in form.AllKeys)
if (formKey.Equals(string.Format("itemquantity{0}", sci.Id), StringComparison.InvariantCultureIgnoreCase))
{
int newQuantity = sci.Quantity;
if (int.TryParse(form[formKey], out newQuantity))
{
var currSciWarnings = _shoppingCartService.UpdateShoppingCartItem(_workContext.CurrentCustomer,
sci.Id, sci.AttributesXml, sci.CustomerEnteredPrice, newQuantity, true);
innerWarnings.Add(sci.Id, currSciWarnings);
}
break;
}
}
}
//updated wishlist
cart = _workContext.CurrentCustomer.ShoppingCartItems
.Where(sci => sci.ShoppingCartType == ShoppingCartType.Wishlist)
.LimitPerStore(_storeContext.CurrentStore.Id)
.ToList();
var model = new WishlistModel();
PrepareWishlistModel(model, cart);
//update current warnings
foreach (var kvp in innerWarnings)
{
//kvp = <cart item identifier, warnings>
var sciId = kvp.Key;
var warnings = kvp.Value;
//find model
var sciModel = model.Items.FirstOrDefault(x => x.Id == sciId);
if (sciModel != null)
foreach (var w in warnings)
if (!sciModel.Warnings.Contains(w))
sciModel.Warnings.Add(w);
}
return View(model);
}
示例4: Wishlist
public ActionResult Wishlist(Guid? customerGuid)
{
if (!_permissionService.Authorize(StandardPermissionProvider.EnableWishlist))
return RedirectToRoute("HomePage");
Customer customer = customerGuid.HasValue ?
_customerService.GetCustomerByGuid(customerGuid.Value)
: _workContext.CurrentCustomer;
if (customer == null)
return RedirectToRoute("HomePage");
var cart = customer.ShoppingCartItems
.Where(sci => sci.ShoppingCartType == ShoppingCartType.Wishlist)
.LimitPerStore(_storeContext.CurrentStore.Id)
.ToList();
var model = new WishlistModel();
PrepareWishlistModel(model, cart, !customerGuid.HasValue);
return View(model);
}
示例5: UpdateWishlistItem
public ActionResult UpdateWishlistItem(FormCollection form)
{
if (!_permissionService.Authorize(StandardPermissionProvider.EnableWishlist))
return RedirectToRoute("HomePage");
//get wishlist cart item identifier
int sciId = 0;
foreach (var formValue in form.AllKeys)
if (formValue.StartsWith("updatecartitem-", StringComparison.InvariantCultureIgnoreCase))
sciId = Convert.ToInt32(formValue.Substring("updatecartitem-".Length));
//get shopping cart item
var cart = _workContext.CurrentCustomer.ShoppingCartItems
.Where(x => x.ShoppingCartType == ShoppingCartType.Wishlist).ToList();
var sci = cart.Where(x => x.Id == sciId).FirstOrDefault();
if (sci == null)
{
return RedirectToRoute("Wishlist");
}
//update the wishlist cart item
var warnings = new List<string>();
foreach (string formKey in form.AllKeys)
if (formKey.Equals(string.Format("itemquantity{0}", sci.Id), StringComparison.InvariantCultureIgnoreCase))
{
int newQuantity = sci.Quantity;
if (int.TryParse(form[formKey], out newQuantity))
{
warnings.AddRange(_shoppingCartService.UpdateShoppingCartItem(_workContext.CurrentCustomer,
sci.Id, newQuantity, true));
}
break;
}
//updated wishlist
cart = _workContext.CurrentCustomer.ShoppingCartItems.Where(x => x.ShoppingCartType == ShoppingCartType.Wishlist).ToList();
var model = new WishlistModel();
PrepareWishlistModel(model, cart);
//update current warnings
//find model
var sciModel = model.Items.Where(x => x.Id == sciId).FirstOrDefault();
if (sciModel != null)
foreach (var w in warnings)
if (!sciModel.Warnings.Contains(w))
sciModel.Warnings.Add(w);
return View(model);
}
示例6: AddOneItemtoCartFromWishlist
public ActionResult AddOneItemtoCartFromWishlist(Guid? customerGuid, FormCollection form)
{
if (!_permissionService.Authorize(StandardPermissionProvider.EnableShoppingCart))
return RedirectToRoute("HomePage");
if (!_permissionService.Authorize(StandardPermissionProvider.EnableWishlist))
return RedirectToRoute("HomePage");
//get wishlist cart item identifier
int sciId = 0;
foreach (var formValue in form.AllKeys)
if (formValue.StartsWith("addtocart-", StringComparison.InvariantCultureIgnoreCase))
sciId = Convert.ToInt32(formValue.Substring("addtocart-".Length));
//get wishlist cart item
var pageCustomer = customerGuid.HasValue
? _customerService.GetCustomerByGuid(customerGuid.Value)
: _workContext.CurrentCustomer;
if (pageCustomer == null)
return RedirectToRoute("HomePage");
var pageCart = pageCustomer.ShoppingCartItems.Where(x => x.ShoppingCartType == ShoppingCartType.Wishlist).ToList();
var sci = pageCart.Where(x => x.Id == sciId).FirstOrDefault();
if (sci == null)
{
return RedirectToRoute("Wishlist");
}
var warnings = _shoppingCartService.AddToCart(_workContext.CurrentCustomer,
sci.ProductVariant, ShoppingCartType.ShoppingCart,
sci.AttributesXml, sci.CustomerEnteredPrice, sci.Quantity, true);
if (_shoppingCartSettings.MoveItemsFromWishlistToCart && //settings enabled
!customerGuid.HasValue && //own wishlist
warnings.Count == 0) //no warnings ( already in the cart)
{
//let's remove the item from wishlist
_shoppingCartService.DeleteShoppingCartItem(sci);
}
if (warnings.Count == 0)
{
//redirect to the shopping cart page
return RedirectToRoute("ShoppingCart");
}
else
{
//no items added. redisplay the wishlist page
var cart = pageCustomer.ShoppingCartItems.Where(x => x.ShoppingCartType == ShoppingCartType.Wishlist).ToList();
var model = new WishlistModel();
PrepareWishlistModel(model, cart, !customerGuid.HasValue);
return View(model);
}
}
示例7: RemoveWishlistItem
public ActionResult RemoveWishlistItem(FormCollection form)
{
if (!_permissionService.Authorize(StandardPermissionProvider.EnableWishlist))
return RedirectToRoute("HomePage");
//get wishlist cart item identifier
int sciId = 0;
foreach (var formValue in form.AllKeys)
if (formValue.StartsWith("removefromcart-", StringComparison.InvariantCultureIgnoreCase))
sciId = Convert.ToInt32(formValue.Substring("removefromcart-".Length));
//get wishlist cart item
var cart = _workContext.CurrentCustomer.ShoppingCartItems
.Where(x => x.ShoppingCartType == ShoppingCartType.Wishlist).ToList();
var sci = cart.Where(x => x.Id == sciId).FirstOrDefault();
if (sci == null)
{
return RedirectToRoute("Wishlist");
}
//remove the wishlist cart item
_shoppingCartService.DeleteShoppingCartItem(sci);
//updated wishlist
cart = _workContext.CurrentCustomer.ShoppingCartItems.Where(x => x.ShoppingCartType == ShoppingCartType.Wishlist).ToList();
var model = new WishlistModel();
PrepareWishlistModel(model, cart);
return View(model);
}
示例8: PrepareWishlistModel
protected WishlistModel PrepareWishlistModel(WishlistModel model, IList<ShoppingCartItem> cart, bool isEditable)
{
if (cart == null)
throw new ArgumentNullException("cart");
if (model == null)
throw new ArgumentNullException("model");
model.EmailWishlistEnabled = _shoppingCartSettings.EmailWishlistEnabled;
model.IsEditable = isEditable;
if (cart.Count == 0)
return model;
#region Simple properties
var customer = cart.FirstOrDefault().Customer;
model.CustomerGuid = customer.CustomerGuid;
model.CustomerFullname = customer.GetFullName();
model.ShowProductImages = _shoppingCartSettings.ShowProductImagesOnShoppingCart;
model.ShowSku = _catalogSettings.ShowProductSku;
//cart warnings
var cartWarnings = _shoppingCartService.GetShoppingCartWarnings(cart, "", false);
foreach (var warning in cartWarnings)
model.Warnings.Add(warning);
#endregion
#region Cart items
foreach (var sci in cart)
{
var cartItemModel = new WishlistModel.ShoppingCartItemModel()
{
Id = sci.Id,
Sku = sci.ProductVariant.Sku,
ProductId = sci.ProductVariant.ProductId,
ProductSeName = sci.ProductVariant.Product.GetSeName(),
Quantity = sci.Quantity,
AttributeInfo = _productAttributeFormatter.FormatAttributes(sci.ProductVariant, sci.AttributesXml),
};
//recurring info
if (sci.ProductVariant.IsRecurring)
cartItemModel.RecurringInfo = string.Format(_localizationService.GetResource("ShoppingCart.RecurringPeriod"), sci.ProductVariant.RecurringCycleLength, sci.ProductVariant.RecurringCyclePeriod.GetLocalizedEnum(_localizationService, _workContext));
//unit prices
if (sci.ProductVariant.CallForPrice)
{
cartItemModel.UnitPrice = _localizationService.GetResource("Products.CallForPrice");
}
else
{
decimal taxRate = decimal.Zero;
decimal shoppingCartUnitPriceWithDiscountBase = _taxService.GetProductPrice(sci.ProductVariant, _priceCalculationService.GetUnitPrice(sci, true), out taxRate);
decimal shoppingCartUnitPriceWithDiscount = _currencyService.ConvertFromPrimaryStoreCurrency(shoppingCartUnitPriceWithDiscountBase, _workContext.WorkingCurrency);
cartItemModel.UnitPrice = _priceFormatter.FormatPrice(shoppingCartUnitPriceWithDiscount);
}
//subtotal, discount
if (sci.ProductVariant.CallForPrice)
{
cartItemModel.SubTotal = _localizationService.GetResource("Products.CallForPrice");
}
else
{
//sub total
decimal taxRate = decimal.Zero;
decimal shoppingCartItemSubTotalWithDiscountBase = _taxService.GetProductPrice(sci.ProductVariant, _priceCalculationService.GetSubTotal(sci, true), out taxRate);
decimal shoppingCartItemSubTotalWithDiscount = _currencyService.ConvertFromPrimaryStoreCurrency(shoppingCartItemSubTotalWithDiscountBase, _workContext.WorkingCurrency);
cartItemModel.SubTotal = _priceFormatter.FormatPrice(shoppingCartItemSubTotalWithDiscount);
//display an applied discount amount
decimal shoppingCartItemSubTotalWithoutDiscountBase = _taxService.GetProductPrice(sci.ProductVariant, _priceCalculationService.GetSubTotal(sci, false), out taxRate);
decimal shoppingCartItemDiscountBase = shoppingCartItemSubTotalWithoutDiscountBase - shoppingCartItemSubTotalWithDiscountBase;
if (shoppingCartItemDiscountBase > decimal.Zero)
{
decimal shoppingCartItemDiscount = _currencyService.ConvertFromPrimaryStoreCurrency(shoppingCartItemDiscountBase, _workContext.WorkingCurrency);
cartItemModel.Discount = _priceFormatter.FormatPrice(shoppingCartItemDiscount);
}
}
//product name
if (!String.IsNullOrEmpty(sci.ProductVariant.GetLocalized(x => x.Name)))
cartItemModel.ProductName = string.Format("{0} ({1})", sci.ProductVariant.Product.GetLocalized(x => x.Name), sci.ProductVariant.GetLocalized(x => x.Name));
else
cartItemModel.ProductName = sci.ProductVariant.Product.GetLocalized(x => x.Name);
//picture
if (_shoppingCartSettings.ShowProductImagesOnShoppingCart)
{
var picture = _pictureService.GetPictureById(sci.ProductVariant.PictureId);
if (picture == null)
{
picture = _pictureService.GetPicturesByProductId(sci.ProductVariant.Product.Id, 1).FirstOrDefault();
}
cartItemModel.Picture = new PictureModel()
{
ImageUrl = _pictureService.GetPictureUrl(picture, _mediaSetting.CartThumbPictureSize, true),
Title = string.Format(_localizationService.GetResource("Media.Product.ImageLinkTitleFormat"), cartItemModel.ProductName),
//.........这里部分代码省略.........
示例9: PrepareWishlistModel
protected virtual void PrepareWishlistModel(WishlistModel model,
IList<ShoppingCartItem> cart, bool isEditable = true)
{
if (cart == null)
throw new ArgumentNullException("cart");
if (model == null)
throw new ArgumentNullException("model");
model.EmailWishlistEnabled = _shoppingCartSettings.EmailWishlistEnabled;
model.IsEditable = isEditable;
model.DisplayAddToCart = _permissionService.Authorize(StandardPermissionProvider.EnableShoppingCart);
model.DisplayTaxShippingInfo = _catalogSettings.DisplayTaxShippingInfoWishlist;
if (!cart.Any())
return;
#region Simple properties
var customer = cart.GetCustomer();
model.CustomerGuid = customer.CustomerGuid;
model.CustomerFullname = customer.GetFullName();
model.ShowProductImages = _shoppingCartSettings.ShowProductImagesOnShoppingCart;
model.ShowSku = _catalogSettings.ShowProductSku;
//cart warnings
var cartWarnings = _shoppingCartService.GetShoppingCartWarnings(cart, "", false);
foreach (var warning in cartWarnings)
model.Warnings.Add(warning);
#endregion
#region Cart items
foreach (var sci in cart)
{
var cartItemModel = new WishlistModel.ShoppingCartItemModel
{
Id = sci.Id,
Sku = sci.Product.FormatSku(sci.AttributesXml, _productAttributeParser),
ProductId = sci.Product.Id,
ProductName = sci.Product.GetLocalized(x => x.Name),
ProductSeName = sci.Product.GetSeName(),
Quantity = sci.Quantity,
AttributeInfo = _productAttributeFormatter.FormatAttributes(sci.Product, sci.AttributesXml),
};
//allow editing?
//1. setting enabled?
//2. simple product?
//3. has attribute or gift card?
//4. visible individually?
cartItemModel.AllowItemEditing = _shoppingCartSettings.AllowCartItemEditing &&
sci.Product.ProductType == ProductType.SimpleProduct &&
(!String.IsNullOrEmpty(cartItemModel.AttributeInfo) || sci.Product.IsGiftCard) &&
sci.Product.VisibleIndividually;
//allowed quantities
var allowedQuantities = sci.Product.ParseAllowedQuantities();
foreach (var qty in allowedQuantities)
{
cartItemModel.AllowedQuantities.Add(new SelectListItem
{
Text = qty.ToString(),
Value = qty.ToString(),
Selected = sci.Quantity == qty
});
}
//recurring info
if (sci.Product.IsRecurring)
cartItemModel.RecurringInfo = string.Format(_localizationService.GetResource("ShoppingCart.RecurringPeriod"), sci.Product.RecurringCycleLength, sci.Product.RecurringCyclePeriod.GetLocalizedEnum(_localizationService, _workContext));
//rental info
if (sci.Product.IsRental)
{
var rentalStartDate = sci.RentalStartDateUtc.HasValue ? sci.Product.FormatRentalDate(sci.RentalStartDateUtc.Value) : "";
var rentalEndDate = sci.RentalEndDateUtc.HasValue ? sci.Product.FormatRentalDate(sci.RentalEndDateUtc.Value) : "";
cartItemModel.RentalInfo = string.Format(_localizationService.GetResource("ShoppingCart.Rental.FormattedDate"),
rentalStartDate, rentalEndDate);
}
//unit prices
if (sci.Product.CallForPrice)
{
cartItemModel.UnitPrice = _localizationService.GetResource("Products.CallForPrice");
}
else
{
decimal taxRate;
decimal shoppingCartUnitPriceWithDiscountBase = _taxService.GetProductPrice(sci.Product, _priceCalculationService.GetUnitPrice(sci), out taxRate);
decimal shoppingCartUnitPriceWithDiscount = _currencyService.ConvertFromPrimaryStoreCurrency(shoppingCartUnitPriceWithDiscountBase, _workContext.WorkingCurrency);
cartItemModel.UnitPrice = _priceFormatter.FormatPrice(shoppingCartUnitPriceWithDiscount);
}
//subtotal, discount
if (sci.Product.CallForPrice)
{
cartItemModel.SubTotal = _localizationService.GetResource("Products.CallForPrice");
}
//.........这里部分代码省略.........
示例10: AddOneItemtoCartFromWishlist
public ActionResult AddOneItemtoCartFromWishlist(Guid? customerGuid, FormCollection form)
{
if (!_permissionService.Authorize(StandardPermissionProvider.EnableShoppingCart))
return RedirectToRoute("HomePage");
if (!_permissionService.Authorize(StandardPermissionProvider.EnableWishlist))
return RedirectToRoute("HomePage");
//get wishlist cart item identifier
int sciId = 0;
foreach (var formValue in form.AllKeys)
if (formValue.StartsWith("addtocart-", StringComparison.InvariantCultureIgnoreCase))
sciId = Convert.ToInt32(formValue.Substring("addtocart-".Length));
//get wishlist cart item
var pageCustomer = customerGuid.HasValue
? _customerService.GetCustomerByGuid(customerGuid.Value)
: _workContext.CurrentCustomer;
if (pageCustomer == null)
return RedirectToRoute("HomePage");
var pageCart = pageCustomer.ShoppingCartItems.Where(x => x.ShoppingCartType == ShoppingCartType.Wishlist).ToList();
var sci = pageCart.Where(x => x.Id == sciId).FirstOrDefault();
if (sci == null)
{
return RedirectToRoute("Wishlist");
}
_shoppingCartService.AddToCart(_workContext.CurrentCustomer,
sci.ProductVariant, ShoppingCartType.ShoppingCart,
sci.AttributesXml, sci.CustomerEnteredPrice, sci.Quantity, true);
//updated wishlist
var cart = pageCustomer.ShoppingCartItems.Where(x => x.ShoppingCartType == ShoppingCartType.Wishlist).ToList();
var model = new WishlistModel();
PrepareWishlistModel(model, cart, !customerGuid.HasValue);
return View(model);
}
示例11: AddItemstoCartFromWishlist
public ActionResult AddItemstoCartFromWishlist(Guid? customerGuid, FormCollection form)
{
if (!_permissionService.Authorize(StandardPermissionProvider.EnableShoppingCart))
return RedirectToRoute("HomePage");
if (!_permissionService.Authorize(StandardPermissionProvider.EnableWishlist))
return RedirectToRoute("HomePage");
var pageCustomer = customerGuid.HasValue
? _customerService.GetCustomerByGuid(customerGuid.Value)
: _workContext.CurrentCustomer;
if (pageCustomer == null)
return RedirectToRoute("HomePage");
var pageCart = pageCustomer.ShoppingCartItems.Where(sci => sci.ShoppingCartType == ShoppingCartType.Wishlist).ToList();
var allIdsToAdd = form["addtocart"] != null ? form["addtocart"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList() : new List<int>();
foreach (var sci in pageCart)
{
if (allIdsToAdd.Contains(sci.Id))
{
_shoppingCartService.AddToCart(_workContext.CurrentCustomer,
sci.ProductVariant, ShoppingCartType.ShoppingCart,
sci.AttributesXml, sci.CustomerEnteredPrice, sci.Quantity, true);
}
}
//updated wishlist
var cart = pageCustomer.ShoppingCartItems.Where(sci => sci.ShoppingCartType == ShoppingCartType.Wishlist).ToList();
var model = new WishlistModel();
PrepareWishlistModel(model, cart, !customerGuid.HasValue);
return View(model);
}