/// <summary> /// Gets discount amount /// </summary> /// <param name="productVariant">Product variant</param> /// <param name="customer">The customer</param> /// <param name="additionalCharge">Additional charge</param> /// <returns>Discount amount</returns> public virtual decimal GetDiscountAmount(ProductVariant productVariant, Customer customer, decimal additionalCharge) { Discount appliedDiscount = null; return GetDiscountAmount(productVariant, customer, additionalCharge, out appliedDiscount); }
public void Can_check_taxExempt_productVariant() { var productVariant = new ProductVariant(); productVariant.IsTaxExempt = true; _taxService.IsTaxExempt(productVariant, null).ShouldEqual(true); productVariant.IsTaxExempt = false; _taxService.IsTaxExempt(productVariant, null).ShouldEqual(false); }
public void Can_parse_required_productvariant_ids() { var productVariant = new ProductVariant { RequiredProductVariantIds = "1, 4,7 ,a," }; var ids = productVariant.ParseRequiredProductVariantIds(); ids.Length.ShouldEqual(3); ids[0].ShouldEqual(1); ids[1].ShouldEqual(4); ids[2].ShouldEqual(7); }
public void Can_parse_allowed_quantities() { var pv = new ProductVariant() { AllowedQuantities = "1, 5,4,10,sdf" }; var result = pv.ParseAllowedQuatities(); result.Length.ShouldEqual(4); result[0].ShouldEqual(1); result[1].ShouldEqual(5); result[2].ShouldEqual(4); result[3].ShouldEqual(10); }
public void Can_get_final_product_price_with_additionalFee() { var productVariant = new ProductVariant { Id = 1, Name = "Product variant name 1", Price = 12.34M, CustomerEntersPrice = false, Published = true, Product = new Product() { Id = 1, Name = "Product name 1", Published = true } }; //customer Customer customer = null; _priceCalcService.GetFinalPrice(productVariant, customer, 5, false, 1).ShouldEqual(17.34M); }
public void Can_render_virtual_gift_cart() { string attributes = _productAttributeParser.AddGiftCardAttribute("", "recipientName 1", "*****@*****.**", "senderName 1", "*****@*****.**", "custom message"); var productVariant = new ProductVariant() { IsGiftCard = true, GiftCardType = GiftCardType.Virtual, }; var customer = new Customer(); string formattedAttributes = _productAttributeFormatter.FormatAttributes(productVariant, attributes, customer, "<br />", false, false, true, true); formattedAttributes.ShouldEqual("From: senderName 1 <*****@*****.**><br />For: recipientName 1 <*****@*****.**>"); }
public void Can_get_shopping_cart_item_unitPrice() { //customer Customer customer = new Customer(); //shopping cart var productVariant1 = new ProductVariant { Id = 1, Name = "Product variant name 1", Price = 12.34M, CustomerEntersPrice = false, Published = true, Product = new Product() { Id = 1, Name = "Product name 1", Published = true } }; var sci1 = new ShoppingCartItem() { Customer = customer, CustomerId = customer.Id, ProductVariant = productVariant1, ProductVariantId = productVariant1.Id, Quantity = 2, }; _priceCalcService.GetUnitPrice(sci1, false).ShouldEqual(12.34); }
public void Can_get_final_product_price_with_tier_prices_by_customerRole() { var productVariant = new ProductVariant { Id = 1, Name = "Product variant name 1", Price = 12.34M, CustomerEntersPrice = false, Published = true, Product = new Product() { Id = 1, Name = "Product name 1", Published = true } }; //customer roles var customerRole1 = new CustomerRole() { Id = 1, Name = "Some role 1", Active = true, }; var customerRole2 = new CustomerRole() { Id = 2, Name = "Some role 2", Active = true, }; //add tier prices productVariant.TierPrices.Add(new TierPrice() { Price = 10, Quantity = 2, ProductVariant = productVariant, CustomerRole = customerRole1 }); productVariant.TierPrices.Add(new TierPrice() { Price = 9, Quantity = 2, ProductVariant = productVariant, CustomerRole = customerRole2 }); productVariant.TierPrices.Add(new TierPrice() { Price = 8, Quantity = 5, ProductVariant = productVariant, CustomerRole = customerRole1 }); productVariant.TierPrices.Add(new TierPrice() { Price = 5, Quantity = 10, ProductVariant = productVariant, CustomerRole = customerRole2 }); //set HasTierPrices property productVariant.HasTierPrices = true; //customer Customer customer = new Customer(); customer.CustomerRoles.Add(customerRole1); _priceCalcService.GetFinalPrice(productVariant, customer, 0, false, 1).ShouldEqual(12.34M); _priceCalcService.GetFinalPrice(productVariant, customer, 0, false, 2).ShouldEqual(10); _priceCalcService.GetFinalPrice(productVariant, customer, 0, false, 3).ShouldEqual(10); _priceCalcService.GetFinalPrice(productVariant, customer, 0, false, 5).ShouldEqual(8); _priceCalcService.GetFinalPrice(productVariant, customer, 0, false, 10).ShouldEqual(8); }
public void Can_get_final_product_price_with_special_price() { var productVariant = new ProductVariant { Id = 1, Name = "Product variant name 1", Price = 12.34M, SpecialPrice = 10.01M, SpecialPriceStartDateTimeUtc = DateTime.UtcNow.AddDays(-1), SpecialPriceEndDateTimeUtc= DateTime.UtcNow.AddDays(1), CustomerEntersPrice = false, Published = true, Product = new Product() { Id = 1, Name = "Product name 1", Published = true } }; _discountService.Expect(ds => ds.GetAllDiscounts(DiscountType.AssignedToCategories)).Return(new List<Discount>()); //customer Customer customer = null; //valid dates _priceCalcService.GetFinalPrice(productVariant, customer, 0, true, 1).ShouldEqual(10.01M); //invalid date productVariant.SpecialPriceStartDateTimeUtc = DateTime.UtcNow.AddDays(1); _priceCalcService.GetFinalPrice(productVariant, customer, 0, true, 1).ShouldEqual(12.34M); //no dates productVariant.SpecialPriceStartDateTimeUtc = null; productVariant.SpecialPriceEndDateTimeUtc = null; _priceCalcService.GetFinalPrice(productVariant, customer, 0, true, 1).ShouldEqual(10.01M); }
protected PictureModel PrepareCartItemPictureModel(ProductVariant productVariant, int pictureSize, bool showDefaultPicture, string productName) { if (productVariant == null) throw new ArgumentNullException("productVariant"); var pictureCacheKey = string.Format(ModelCacheEventConsumer.CART_PICTURE_MODEL_KEY, productVariant.Id, pictureSize, true, _workContext.WorkingLanguage.Id, _webHelper.IsCurrentConnectionSecured(), _storeContext.CurrentStore.Id); var model = _cacheManager.Get(pictureCacheKey, () => { //first try to load product variant piture var picture = _pictureService.GetPictureById(productVariant.PictureId); if (picture == null) { //if product variant doesn't have any picture assigned, then load product picture picture = _pictureService.GetPicturesByProductId(productVariant.Product.Id, 1).FirstOrDefault(); } return new PictureModel() { ImageUrl = _pictureService.GetPictureUrl(picture, pictureSize, showDefaultPicture), Title = string.Format(_localizationService.GetResource("Media.Product.ImageLinkTitleFormat"), productName), AlternateText = string.Format(_localizationService.GetResource("Media.Product.ImageAlternateTextFormat"), productName), }; }); return model; }
/// <summary> /// Inserts a product variant /// </summary> /// <param name="productVariant">The product variant</param> public virtual void InsertProductVariant(ProductVariant productVariant) { if (productVariant == null) throw new ArgumentNullException("productVariant"); _productVariantRepository.Insert(productVariant); _cacheManager.RemoveByPattern(PRODUCTS_PATTERN_KEY); _cacheManager.RemoveByPattern(PRODUCTVARIANTS_PATTERN_KEY); //event notification _eventPublisher.EntityInserted(productVariant); }
/// <summary> /// Delete a product variant /// </summary> /// <param name="productVariant">Product variant</param> public virtual void DeleteProductVariant(ProductVariant productVariant) { if (productVariant == null) throw new ArgumentNullException("productVariant"); productVariant.Deleted = true; UpdateProductVariant(productVariant); }
/// <summary> /// Adjusts inventory /// </summary> /// <param name="productVariant">Product variant</param> /// <param name="decrease">A value indicating whether to increase or descrease product variant stock quantity</param> /// <param name="quantity">Quantity</param> /// <param name="attributesXml">Attributes in XML format</param> public virtual void AdjustInventory(ProductVariant productVariant, bool decrease, int quantity, string attributesXml) { if (productVariant == null) throw new ArgumentNullException("productVariant"); var prevStockQuantity = productVariant.StockQuantity; switch (productVariant.ManageInventoryMethod) { case ManageInventoryMethod.DontManageStock: { //do nothing return; } case ManageInventoryMethod.ManageStock: { int newStockQuantity = 0; if (decrease) newStockQuantity = productVariant.StockQuantity - quantity; else newStockQuantity = productVariant.StockQuantity + quantity; bool newPublished = productVariant.Published; bool newDisableBuyButton = productVariant.DisableBuyButton; bool newDisableWishlistButton = productVariant.DisableWishlistButton; //check if minimum quantity is reached if (decrease) { if (productVariant.MinStockQuantity >= newStockQuantity) { switch (productVariant.LowStockActivity) { case LowStockActivity.DisableBuyButton: newDisableBuyButton = true; newDisableWishlistButton = true; break; case LowStockActivity.Unpublish: newPublished = false; break; default: break; } } } productVariant.StockQuantity = newStockQuantity; productVariant.DisableBuyButton = newDisableBuyButton; productVariant.DisableWishlistButton = newDisableWishlistButton; productVariant.Published = newPublished; UpdateProductVariant(productVariant); //send email notification if (decrease && productVariant.NotifyAdminForQuantityBelow > newStockQuantity) _workflowMessageService.SendQuantityBelowStoreOwnerNotification(productVariant, _localizationSettings.DefaultAdminLanguageId); if (decrease) { var product = productVariant.Product; bool allProductVariantsUnpublished = true; foreach (var pv2 in GetProductVariantsByProductId(product.Id)) { if (pv2.Published) { allProductVariantsUnpublished = false; break; } } if (allProductVariantsUnpublished) { product.Published = false; UpdateProduct(product); } } } break; case ManageInventoryMethod.ManageStockByAttributes: { var combination = _productAttributeParser.FindProductVariantAttributeCombination(productVariant, attributesXml); if (combination != null) { int newStockQuantity = 0; if (decrease) newStockQuantity = combination.StockQuantity - quantity; else newStockQuantity = combination.StockQuantity + quantity; combination.StockQuantity = newStockQuantity; _productAttributeService.UpdateProductVariantAttributeCombination(combination); } } break; default: break; } //TODO send back in stock notifications? //if (productVariant.ManageInventoryMethod == ManageInventoryMethod.ManageStock && // productVariant.BackorderMode == BackorderMode.NoBackorders && // productVariant.AllowBackInStockSubscriptions && // productVariant.StockQuantity > 0 && // prevStockQuantity <= 0 && // productVariant.Published && // !productVariant.Deleted) //{ // //_backInStockSubscriptionService.SendNotificationsToSubscribers(productVariant); //} }
/// <summary> /// Update HasTierPrices property (used for performance optimization) /// </summary> /// <param name="productVariant">Product variant</param> public virtual void UpdateHasTierPricesProperty(ProductVariant productVariant) { if (productVariant == null) throw new ArgumentNullException("productVariant"); productVariant.HasTierPrices = productVariant.TierPrices.Count > 0; UpdateProductVariant(productVariant); }
/// <summary> /// Update HasDiscountsApplied property (used for performance optimization) /// </summary> /// <param name="productVariant">Product variant</param> public virtual void UpdateHasDiscountsApplied(ProductVariant productVariant) { if (productVariant == null) throw new ArgumentNullException("productVariant"); productVariant.HasDiscountsApplied = productVariant.AppliedDiscounts.Count > 0; UpdateProductVariant(productVariant); }
/// <summary> /// Validates shopping cart item /// </summary> /// <param name="customer">Customer</param> /// <param name="shoppingCartType">Shopping cart type</param> /// <param name="productVariant">Product variant</param> /// <param name="storeId">Store identifier</param> /// <param name="selectedAttributes">Selected attributes</param> /// <param name="customerEnteredPrice">Customer entered price</param> /// <param name="quantity">Quantity</param> /// <param name="automaticallyAddRequiredProductVariantsIfEnabled">Automatically add required product variants if enabled</param> /// <param name="getStandardWarnings">A value indicating whether we should validate a product variant for standard properties</param> /// <param name="getAttributesWarnings">A value indicating whether we should validate product attributes</param> /// <param name="getGiftCardWarnings">A value indicating whether we should validate gift card properties</param> /// <param name="getRequiredProductVariantWarnings">A value indicating whether we should validate required product variants (product variants which require other variant to be added to the cart)</param> /// <returns>Warnings</returns> public virtual IList<string> GetShoppingCartItemWarnings(Customer customer, ShoppingCartType shoppingCartType, ProductVariant productVariant, int storeId, string selectedAttributes, decimal customerEnteredPrice, int quantity, bool automaticallyAddRequiredProductVariantsIfEnabled, bool getStandardWarnings = true, bool getAttributesWarnings = true, bool getGiftCardWarnings = true, bool getRequiredProductVariantWarnings = true) { if (productVariant == null) throw new ArgumentNullException("productVariant"); var warnings = new List<string>(); //standard properties if (getStandardWarnings) warnings.AddRange(GetStandardWarnings(customer, shoppingCartType, productVariant, selectedAttributes, customerEnteredPrice, quantity)); //selected attributes if (getAttributesWarnings) warnings.AddRange(GetShoppingCartItemAttributeWarnings(shoppingCartType, productVariant, selectedAttributes)); //gift cards if (getGiftCardWarnings) warnings.AddRange(GetShoppingCartItemGiftCardWarnings(shoppingCartType, productVariant, selectedAttributes)); //required product variants if (getRequiredProductVariantWarnings) warnings.AddRange(GetRequiredProductVariantWarnings(customer, shoppingCartType, productVariant, storeId, automaticallyAddRequiredProductVariantsIfEnabled)); return warnings; }
/// <summary> /// Validates a product variant for standard properties /// </summary> /// <param name="customer">Customer</param> /// <param name="shoppingCartType">Shopping cart type</param> /// <param name="productVariant">Product variant</param> /// <param name="selectedAttributes">Selected attributes</param> /// <param name="customerEnteredPrice">Customer entered price</param> /// <param name="quantity">Quantity</param> /// <returns>Warnings</returns> public virtual IList<string> GetStandardWarnings(Customer customer, ShoppingCartType shoppingCartType, ProductVariant productVariant, string selectedAttributes, decimal customerEnteredPrice, int quantity) { if (customer == null) throw new ArgumentNullException("customer"); if (productVariant == null) throw new ArgumentNullException("productVariant"); var warnings = new List<string>(); var product = productVariant.Product; if (product == null) { warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.CannotLoadProduct"), productVariant.ProductId)); return warnings; } //deleted? if (product.Deleted || productVariant.Deleted) { warnings.Add(_localizationService.GetResource("ShoppingCart.ProductDeleted")); return warnings; } //published? if (!product.Published || !productVariant.Published) { warnings.Add(_localizationService.GetResource("ShoppingCart.ProductUnpublished")); } //ACL if (!_aclService.Authorize(product, customer)) { warnings.Add(_localizationService.GetResource("ShoppingCart.ProductUnpublished")); } //Store mapping if (!_storeMappingService.Authorize(product, _storeContext.CurrentStore)) { warnings.Add(_localizationService.GetResource("ShoppingCart.ProductUnpublished")); } //disabled "add to cart" button if (shoppingCartType == ShoppingCartType.ShoppingCart && productVariant.DisableBuyButton) { warnings.Add(_localizationService.GetResource("ShoppingCart.BuyingDisabled")); } //disabled "add to wishlist" button if (shoppingCartType == ShoppingCartType.Wishlist && productVariant.DisableWishlistButton) { warnings.Add(_localizationService.GetResource("ShoppingCart.WishlistDisabled")); } //call for price if (shoppingCartType == ShoppingCartType.ShoppingCart && productVariant.CallForPrice) { warnings.Add(_localizationService.GetResource("Products.CallForPrice")); } //customer entered price if (productVariant.CustomerEntersPrice) { if (customerEnteredPrice < productVariant.MinimumCustomerEnteredPrice || customerEnteredPrice > productVariant.MaximumCustomerEnteredPrice) { decimal minimumCustomerEnteredPrice = _currencyService.ConvertFromPrimaryStoreCurrency(productVariant.MinimumCustomerEnteredPrice, _workContext.WorkingCurrency); decimal maximumCustomerEnteredPrice = _currencyService.ConvertFromPrimaryStoreCurrency(productVariant.MaximumCustomerEnteredPrice, _workContext.WorkingCurrency); warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.CustomerEnteredPrice.RangeError"), _priceFormatter.FormatPrice(minimumCustomerEnteredPrice, false, false), _priceFormatter.FormatPrice(maximumCustomerEnteredPrice, false, false))); } } //quantity validation var hasQtyWarnings = false; if (quantity < productVariant.OrderMinimumQuantity) { warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.MinimumQuantity"), productVariant.OrderMinimumQuantity)); hasQtyWarnings = true; } if (quantity > productVariant.OrderMaximumQuantity) { warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.MaximumQuantity"), productVariant.OrderMaximumQuantity)); hasQtyWarnings = true; } var allowedQuantities = productVariant.ParseAllowedQuatities(); if (allowedQuantities.Length > 0 && !allowedQuantities.Contains(quantity)) { warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.AllowedQuantities"), string.Join(", ", allowedQuantities))); } var validateOutOfStock = shoppingCartType == ShoppingCartType.ShoppingCart || !_shoppingCartSettings.AllowOutOfStockItemsToBeAddedToWishlist; if (validateOutOfStock && !hasQtyWarnings) { switch (productVariant.ManageInventoryMethod) { case ManageInventoryMethod.DontManageStock: { } break; case ManageInventoryMethod.ManageStock: { if ((BackorderMode)productVariant.BackorderMode == BackorderMode.NoBackorders) { if (productVariant.StockQuantity < quantity) { int maximumQuantityCanBeAdded = productVariant.StockQuantity; if (maximumQuantityCanBeAdded <= 0) warnings.Add(_localizationService.GetResource("ShoppingCart.OutOfStock")); else warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.QuantityExceedsStock"), maximumQuantityCanBeAdded)); } } } break; case ManageInventoryMethod.ManageStockByAttributes: { var combination = productVariant .ProductVariantAttributeCombinations .FirstOrDefault(x => _productAttributeParser.AreProductAttributesEqual(x.AttributesXml, selectedAttributes)); if (combination != null && !combination.AllowOutOfStockOrders && combination.StockQuantity < quantity) { int maximumQuantityCanBeAdded = combination.StockQuantity; if (maximumQuantityCanBeAdded <= 0) warnings.Add(_localizationService.GetResource("ShoppingCart.OutOfStock")); else warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.QuantityExceedsStock"), maximumQuantityCanBeAdded)); } } break; default: break; } } //availability dates bool availableStartDateError = false; if (productVariant.AvailableStartDateTimeUtc.HasValue) { DateTime now = DateTime.UtcNow; DateTime availableStartDateTime = DateTime.SpecifyKind(productVariant.AvailableStartDateTimeUtc.Value, DateTimeKind.Utc); if (availableStartDateTime.CompareTo(now) > 0) { warnings.Add(_localizationService.GetResource("ShoppingCart.NotAvailable")); availableStartDateError = true; } } if (productVariant.AvailableEndDateTimeUtc.HasValue && !availableStartDateError) { DateTime now = DateTime.UtcNow; DateTime availableEndDateTime = DateTime.SpecifyKind(productVariant.AvailableEndDateTimeUtc.Value, DateTimeKind.Utc); if (availableEndDateTime.CompareTo(now) < 0) { warnings.Add(_localizationService.GetResource("ShoppingCart.NotAvailable")); } } return warnings; }
public static ProductVariant ToEntity(this ProductVariantModel model, ProductVariant destination) { return Mapper.Map(model, destination); }
/// <summary> /// Sends a "quantity below" notification to a store owner /// </summary> /// <param name="productVariant">Product variant</param> /// <param name="languageId">Message language identifier</param> /// <returns>Queued email identifier</returns> public virtual int SendQuantityBelowStoreOwnerNotification(ProductVariant productVariant, int languageId) { if (productVariant == null) throw new ArgumentNullException("productVariant"); var store = _storeContext.CurrentStore; languageId = EnsureLanguageIsActive(languageId, store.Id); var messageTemplate = GetLocalizedActiveMessageTemplate("QuantityBelow.StoreOwnerNotification", languageId, store.Id); if (messageTemplate == null) return 0; var tokens = new List<Token>(); _messageTokenProvider.AddStoreTokens(tokens, store); _messageTokenProvider.AddProductVariantTokens(tokens, productVariant); //event notification _eventPublisher.MessageTokensAdded(messageTemplate, tokens); var emailAccount = GetEmailAccountOfMessageTemplate(messageTemplate, languageId); var toEmail = emailAccount.Email; var toName = emailAccount.DisplayName; return SendNotification(messageTemplate, emailAccount, languageId, tokens, toEmail, toName); }
public virtual void AddProductVariantTokens(IList<Token> tokens, ProductVariant productVariant) { tokens.Add(new Token("ProductVariant.ID", productVariant.Id.ToString())); tokens.Add(new Token("ProductVariant.FullProductName", productVariant.FullProductName)); tokens.Add(new Token("ProductVariant.StockQuantity", productVariant.StockQuantity.ToString())); //event notification _eventPublisher.EntityTokensAdded(productVariant, tokens); }
public void Can_get_final_product_price_with_tier_prices() { var productVariant = new ProductVariant { Id = 1, Name = "Product variant name 1", Price = 12.34M, CustomerEntersPrice = false, Published = true, Product = new Product() { Id = 1, Name = "Product name 1", Published = true } }; //add tier prices productVariant.TierPrices.Add(new TierPrice() { Price = 10, Quantity = 2, ProductVariant = productVariant }); productVariant.TierPrices.Add(new TierPrice() { Price = 8, Quantity = 5, ProductVariant = productVariant }); //set HasTierPrices property productVariant.HasTierPrices = true; //customer Customer customer = null; _priceCalcService.GetFinalPrice(productVariant, customer, 0, false, 1).ShouldEqual(12.34M); _priceCalcService.GetFinalPrice(productVariant, customer, 0, false, 2).ShouldEqual(10); _priceCalcService.GetFinalPrice(productVariant, customer, 0, false, 3).ShouldEqual(10); _priceCalcService.GetFinalPrice(productVariant, customer, 0, false, 5).ShouldEqual(8); }
/// <summary> /// Add a product variant to shopping cart /// </summary> /// <param name="customer">Customer</param> /// <param name="productVariant">Product variant</param> /// <param name="shoppingCartType">Shopping 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="automaticallyAddRequiredProductVariantsIfEnabled">Automatically add required product variants if enabled</param> /// <returns>Warnings</returns> public virtual IList<string> AddToCart(Customer customer, ProductVariant productVariant, ShoppingCartType shoppingCartType, int storeId, string selectedAttributes, decimal customerEnteredPrice, int quantity, bool automaticallyAddRequiredProductVariantsIfEnabled) { if (customer == null) throw new ArgumentNullException("customer"); if (productVariant == null) throw new ArgumentNullException("productVariant"); var warnings = new List<string>(); if (shoppingCartType == ShoppingCartType.ShoppingCart && !_permissionService.Authorize(StandardPermissionProvider.EnableShoppingCart, customer)) { warnings.Add("Shopping cart is disabled"); return warnings; } if (shoppingCartType == ShoppingCartType.Wishlist && !_permissionService.Authorize(StandardPermissionProvider.EnableWishlist, customer)) { warnings.Add("Wishlist is disabled"); return warnings; } if (quantity <= 0) { warnings.Add(_localizationService.GetResource("ShoppingCart.QuantityShouldPositive")); return warnings; } //reset checkout info _customerService.ResetCheckoutData(customer, storeId); var cart = customer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == shoppingCartType) .Where(sci => sci.StoreId == storeId) .ToList(); var shoppingCartItem = FindShoppingCartItemInTheCart(cart, shoppingCartType, productVariant, selectedAttributes, customerEnteredPrice); if (shoppingCartItem != null) { //update existing shopping cart item int newQuantity = shoppingCartItem.Quantity + quantity; warnings.AddRange(GetShoppingCartItemWarnings(customer, shoppingCartType, productVariant, storeId, selectedAttributes, customerEnteredPrice, newQuantity, automaticallyAddRequiredProductVariantsIfEnabled)); if (warnings.Count == 0) { shoppingCartItem.AttributesXml = selectedAttributes; shoppingCartItem.Quantity = newQuantity; shoppingCartItem.UpdatedOnUtc = DateTime.UtcNow; _customerService.UpdateCustomer(customer); //event notification _eventPublisher.EntityUpdated(shoppingCartItem); } } else { //new shopping cart item warnings.AddRange(GetShoppingCartItemWarnings(customer, shoppingCartType, productVariant, storeId, selectedAttributes, customerEnteredPrice, quantity, automaticallyAddRequiredProductVariantsIfEnabled)); if (warnings.Count == 0) { //maximum items validation switch (shoppingCartType) { case ShoppingCartType.ShoppingCart: { if (cart.Count >= _shoppingCartSettings.MaximumShoppingCartItems) { warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.MaximumShoppingCartItems"), _shoppingCartSettings.MaximumShoppingCartItems)); return warnings; } } break; case ShoppingCartType.Wishlist: { if (cart.Count >= _shoppingCartSettings.MaximumWishlistItems) { warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.MaximumWishlistItems"), _shoppingCartSettings.MaximumWishlistItems)); return warnings; } } break; default: break; } DateTime now = DateTime.UtcNow; shoppingCartItem = new ShoppingCartItem() { ShoppingCartType = shoppingCartType, StoreId = storeId, ProductVariant = productVariant, AttributesXml = selectedAttributes, CustomerEnteredPrice = customerEnteredPrice, Quantity = quantity, CreatedOnUtc = now, UpdatedOnUtc = now }; customer.ShoppingCartItems.Add(shoppingCartItem); _customerService.UpdateCustomer(customer); //event notification _eventPublisher.EntityInserted(shoppingCartItem); } } return warnings; }
public void Can_get_product_discount() { var productVariant = new ProductVariant { Id = 1, Name = "Product variant name 1", Price = 12.34M, CustomerEntersPrice = false, Published = true, Product = new Product() { Id = 1, Name = "Product name 1", Published = true } }; //customer Customer customer = null; //discounts var discount1 = new Discount() { Id = 1, Name = "Discount 1", DiscountType = DiscountType.AssignedToSkus, DiscountAmount = 3, DiscountLimitation = DiscountLimitationType.Unlimited }; discount1.AppliedToProductVariants.Add(productVariant); productVariant.AppliedDiscounts.Add(discount1); //set HasDiscountsApplied property productVariant.HasDiscountsApplied = true; _discountService.Expect(ds => ds.IsDiscountValid(discount1, customer)).Return(true); _discountService.Expect(ds => ds.GetAllDiscounts(DiscountType.AssignedToCategories)).Return(new List<Discount>()); var discount2 = new Discount() { Id = 2, Name = "Discount 2", DiscountType = DiscountType.AssignedToSkus, DiscountAmount = 4, DiscountLimitation = DiscountLimitationType.Unlimited }; discount2.AppliedToProductVariants.Add(productVariant); productVariant.AppliedDiscounts.Add(discount2); _discountService.Expect(ds => ds.IsDiscountValid(discount2, customer)).Return(true); var discount3 = new Discount() { Id = 3, Name = "Discount 3", DiscountType = DiscountType.AssignedToOrderSubTotal, DiscountAmount = 5, DiscountLimitation = DiscountLimitationType.Unlimited, RequiresCouponCode = true, CouponCode = "SECRET CODE" }; discount3.AppliedToProductVariants.Add(productVariant); productVariant.AppliedDiscounts.Add(discount3); //discount is not valid _discountService.Expect(ds => ds.IsDiscountValid(discount3, customer)).Return(false); Discount appliedDiscount; _priceCalcService.GetDiscountAmount(productVariant, customer, 0, 1, out appliedDiscount).ShouldEqual(4); appliedDiscount.ShouldNotBeNull(); appliedDiscount.ShouldEqual(discount2); }
/// <summary> /// Finds a shopping cart item in the cart /// </summary> /// <param name="shoppingCart">Shopping cart</param> /// <param name="shoppingCartType">Shopping cart type</param> /// <param name="productVariant">Product variant</param> /// <param name="selectedAttributes">Selected attributes</param> /// <param name="customerEnteredPrice">Price entered by a customer</param> /// <returns>Found shopping cart item</returns> public virtual ShoppingCartItem FindShoppingCartItemInTheCart(IList<ShoppingCartItem> shoppingCart, ShoppingCartType shoppingCartType, ProductVariant productVariant, string selectedAttributes = "", decimal customerEnteredPrice = decimal.Zero) { if (shoppingCart == null) throw new ArgumentNullException("shoppingCart"); if (productVariant == null) throw new ArgumentNullException("productVariant"); foreach (var sci in shoppingCart.Where(a => a.ShoppingCartType == shoppingCartType)) { if (sci.ProductVariantId == productVariant.Id) { //attributes bool attributesEqual = _productAttributeParser.AreProductAttributesEqual(sci.AttributesXml, selectedAttributes); //gift cards bool giftCardInfoSame = true; if (sci.ProductVariant.IsGiftCard) { string giftCardRecipientName1 = string.Empty; string giftCardRecipientEmail1 = string.Empty; string giftCardSenderName1 = string.Empty; string giftCardSenderEmail1 = string.Empty; string giftCardMessage1 = string.Empty; _productAttributeParser.GetGiftCardAttribute(selectedAttributes, out giftCardRecipientName1, out giftCardRecipientEmail1, out giftCardSenderName1, out giftCardSenderEmail1, out giftCardMessage1); string giftCardRecipientName2 = string.Empty; string giftCardRecipientEmail2 = string.Empty; string giftCardSenderName2 = string.Empty; string giftCardSenderEmail2 = string.Empty; string giftCardMessage2 = string.Empty; _productAttributeParser.GetGiftCardAttribute(sci.AttributesXml, out giftCardRecipientName2, out giftCardRecipientEmail2, out giftCardSenderName2, out giftCardSenderEmail2, out giftCardMessage2); if (giftCardRecipientName1.ToLowerInvariant() != giftCardRecipientName2.ToLowerInvariant() || giftCardSenderName1.ToLowerInvariant() != giftCardSenderName2.ToLowerInvariant()) giftCardInfoSame = false; } //price is the same (for products which require customers to enter a price) bool customerEnteredPricesEqual = true; if (sci.ProductVariant.CustomerEntersPrice) customerEnteredPricesEqual = Math.Round(sci.CustomerEnteredPrice, 2) == Math.Round(customerEnteredPrice, 2); //found? if (attributesEqual && giftCardInfoSame && customerEnteredPricesEqual) return sci; } } return null; }
public void Ensure_discount_is_not_applied_to_products_with_prices_entered_by_customer() { var productVariant = new ProductVariant { Id = 1, Name = "Product variant name 1", Price = 12.34M, CustomerEntersPrice = true, Published = true, Product = new Product() { Id = 1, Name = "Product name 1", Published = true } }; //customer Customer customer = null; //discounts var discount1 = new Discount() { Id = 1, Name = "Discount 1", DiscountType = DiscountType.AssignedToSkus, DiscountAmount = 3, DiscountLimitation = DiscountLimitationType.Unlimited }; discount1.AppliedToProductVariants.Add(productVariant); productVariant.AppliedDiscounts.Add(discount1); _discountService.Expect(ds => ds.IsDiscountValid(discount1, customer)).Return(true); Discount appliedDiscount; _priceCalcService.GetDiscountAmount(productVariant, customer, 0, 1, out appliedDiscount).ShouldEqual(0); appliedDiscount.ShouldBeNull(); }
/// <summary> /// Validates required product variants (product variants which require other variant to be added to the cart) /// </summary> /// <param name="customer">Customer</param> /// <param name="shoppingCartType">Shopping cart type</param> /// <param name="productVariant">Product variant</param> /// <param name="storeId">Store identifier</param> /// <param name="automaticallyAddRequiredProductVariantsIfEnabled">Automatically add required product variants if enabled</param> /// <returns>Warnings</returns> public virtual IList<string> GetRequiredProductVariantWarnings(Customer customer, ShoppingCartType shoppingCartType, ProductVariant productVariant, int storeId, bool automaticallyAddRequiredProductVariantsIfEnabled) { if (customer == null) throw new ArgumentNullException("customer"); if (productVariant == null) throw new ArgumentNullException("productVariant"); var cart = customer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == shoppingCartType) .Where(sci => sci.StoreId == storeId) .ToList(); var warnings = new List<string>(); if (productVariant.RequireOtherProducts) { var requiredProductVariants = new List<ProductVariant>(); foreach (var id in productVariant.ParseRequiredProductVariantIds()) { var rpv = _productService.GetProductVariantById(id); if (rpv != null) requiredProductVariants.Add(rpv); } foreach (var rpv in requiredProductVariants) { //ensure that product is in the cart bool alreadyInTheCart = false; foreach (var sci in cart) { if (sci.ProductVariantId == rpv.Id) { alreadyInTheCart = true; break; } } //not in the cart if (!alreadyInTheCart) { string fullProductName = ""; if (!String.IsNullOrEmpty(rpv.GetLocalized(x => x.Name))) fullProductName = string.Format("{0} ({1})", rpv.Product.GetLocalized(x => x.Name), rpv.GetLocalized(x => x.Name)); else fullProductName = rpv.Product.GetLocalized(x => x.Name); if (productVariant.AutomaticallyAddRequiredProductVariants) { //add to cart (if possible) if (automaticallyAddRequiredProductVariantsIfEnabled) { //pass 'false' for 'automaticallyAddRequiredProductVariantsIfEnabled' to prevent circular references var addToCartWarnings = AddToCart(customer, rpv, shoppingCartType, storeId, "", decimal.Zero, 1, false); if (addToCartWarnings.Count > 0) { //a product wasn't atomatically added for some reasons //don't display specific errors from 'addToCartWarnings' variable //display only generic error warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.RequiredProductWarning"), fullProductName)); } } else { warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.RequiredProductWarning"), fullProductName)); } } else { warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.RequiredProductWarning"), fullProductName)); } } } } return warnings; }
public void Can_get_final_product_price_with_discount() { var productVariant = new ProductVariant { Id = 1, Name = "Product variant name 1", Price = 12.34M, CustomerEntersPrice = false, Published = true, Product = new Product() { Id = 1, Name = "Product name 1", Published = true } }; //customer Customer customer = null; //discounts var discount1 = new Discount() { Id = 1, Name = "Discount 1", DiscountType = DiscountType.AssignedToSkus, DiscountAmount = 3, DiscountLimitation = DiscountLimitationType.Unlimited }; discount1.AppliedToProductVariants.Add(productVariant); productVariant.AppliedDiscounts.Add(discount1); //set HasDiscountsApplied property productVariant.HasDiscountsApplied = true; _discountService.Expect(ds => ds.IsDiscountValid(discount1, customer)).Return(true); _discountService.Expect(ds => ds.GetAllDiscounts(DiscountType.AssignedToCategories)).Return(new List<Discount>()); _priceCalcService.GetFinalPrice(productVariant, customer, 0, true, 1).ShouldEqual(9.34M); }
/// <summary> /// Validates shopping cart item attributes /// </summary> /// <param name="shoppingCartType">Shopping cart type</param> /// <param name="productVariant">Product variant</param> /// <param name="selectedAttributes">Selected attributes</param> /// <returns>Warnings</returns> public virtual IList<string> GetShoppingCartItemAttributeWarnings(ShoppingCartType shoppingCartType, ProductVariant productVariant, string selectedAttributes) { if (productVariant == null) throw new ArgumentNullException("productVariant"); var warnings = new List<string>(); //selected attributes var pva1Collection = _productAttributeParser.ParseProductVariantAttributes(selectedAttributes); foreach (var pva1 in pva1Collection) { var pv1 = pva1.ProductVariant; if (pv1 != null) { if (pv1.Id != productVariant.Id) { warnings.Add("Attribute error"); } } else { warnings.Add("Attribute error"); return warnings; } } //existing product attributes var pva2Collection = productVariant.ProductVariantAttributes; foreach (var pva2 in pva2Collection) { if (pva2.IsRequired) { bool found = false; //selected product attributes foreach (var pva1 in pva1Collection) { if (pva1.Id == pva2.Id) { var pvaValuesStr = _productAttributeParser.ParseValues(selectedAttributes, pva1.Id); foreach (string str1 in pvaValuesStr) { if (!String.IsNullOrEmpty(str1.Trim())) { found = true; break; } } } } //if not found if (!found) { if (!string.IsNullOrEmpty(pva2.TextPrompt)) { warnings.Add(pva2.TextPrompt); } else { warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.SelectAttribute"), pva2.ProductAttribute.GetLocalized(a => a.Name))); } } } } return warnings; }
public void Can_render_attributes_withoutPrices() { string attributes = ""; //color: green attributes = _productAttributeParser.AddProductAttribute(attributes, pva1_1, pvav1_1.Id.ToString()); //custom option: option 1, option 2 attributes = _productAttributeParser.AddProductAttribute(attributes, pva2_1, pvav2_1.Id.ToString()); attributes = _productAttributeParser.AddProductAttribute(attributes, pva2_1, pvav2_2.Id.ToString()); //custom text attributes = _productAttributeParser.AddProductAttribute(attributes, pva3_1, "Some custom text goes here"); //gift card attributes attributes = _productAttributeParser.AddGiftCardAttribute(attributes, "recipientName 1", "*****@*****.**", "senderName 1", "*****@*****.**", "custom message"); var productVariant = new ProductVariant() { IsGiftCard = true, GiftCardType = GiftCardType.Virtual, }; var customer = new Customer(); string formattedAttributes = _productAttributeFormatter.FormatAttributes(productVariant, attributes, customer, "<br />", false, false, true, true); formattedAttributes.ShouldEqual("Color: Green<br />Some custom option: Option 1<br />Some custom option: Option 2<br />Color: Some custom text goes here<br />From: senderName 1 <*****@*****.**><br />For: recipientName 1 <*****@*****.**>"); }
/// <summary> /// Validates shopping cart item (gift card) /// </summary> /// <param name="shoppingCartType">Shopping cart type</param> /// <param name="productVariant">Product variant</param> /// <param name="selectedAttributes">Selected attributes</param> /// <returns>Warnings</returns> public virtual IList<string> GetShoppingCartItemGiftCardWarnings(ShoppingCartType shoppingCartType, ProductVariant productVariant, string selectedAttributes) { if (productVariant == null) throw new ArgumentNullException("productVariant"); var warnings = new List<string>(); //gift cards if (productVariant.IsGiftCard) { string giftCardRecipientName = string.Empty; string giftCardRecipientEmail = string.Empty; string giftCardSenderName = string.Empty; string giftCardSenderEmail = string.Empty; string giftCardMessage = string.Empty; _productAttributeParser.GetGiftCardAttribute(selectedAttributes, out giftCardRecipientName, out giftCardRecipientEmail, out giftCardSenderName, out giftCardSenderEmail, out giftCardMessage); if (String.IsNullOrEmpty(giftCardRecipientName)) warnings.Add(_localizationService.GetResource("ShoppingCart.RecipientNameError")); if (productVariant.GiftCardType == GiftCardType.Virtual) { //validate for virtual gift cards only if (String.IsNullOrEmpty(giftCardRecipientEmail) || !CommonHelper.IsValidEmail(giftCardRecipientEmail)) warnings.Add(_localizationService.GetResource("ShoppingCart.RecipientEmailError")); } if (String.IsNullOrEmpty(giftCardSenderName)) warnings.Add(_localizationService.GetResource("ShoppingCart.SenderNameError")); if (productVariant.GiftCardType == GiftCardType.Virtual) { //validate for virtual gift cards only if (String.IsNullOrEmpty(giftCardSenderEmail) || !CommonHelper.IsValidEmail(giftCardSenderEmail)) warnings.Add(_localizationService.GetResource("ShoppingCart.SenderEmailError")); } } return warnings; }