public async Task <IActionResult> StoreSelected(string shopId) { Shop shop = await _shopService.GetShopByIdAsync(Int32.Parse(shopId)); _customerShopService.InsertOrUpdateCustomerShop((await _workContext.GetCurrentCustomerAsync()).Id, Int32.Parse(shopId)); var shoppingCartItems = (await _shoppingCartService.GetShoppingCartAsync(await _workContext.GetCurrentCustomerAsync(), ShoppingCartType.ShoppingCart)) .Select(sci => sci); // update all attribute definitions foreach (var cartItem in shoppingCartItems) { // update attribute definitions to include the proper name ProductAttributeMapping pickupAttribute = await _attributeUtilities.GetPickupAttributeMappingAsync(cartItem.AttributesXml); if (pickupAttribute != null) { // check if product is available at the selected store StockResponse stockResponse = await _backendStockService.GetApiStockAsync(cartItem.ProductId); bool available = false; if (stockResponse != null) { available = stockResponse.ProductStocks.Where(ps => ps.Available && ps.Shop.Id == shop.Id).Any(); } if (available) { string removedAttr = _productAttributeParser.RemoveProductAttribute(cartItem.AttributesXml, pickupAttribute); cartItem.AttributesXml = await _attributeUtilities.InsertPickupAttributeAsync( await _productService.GetProductByIdAsync(cartItem.ProductId), stockResponse, removedAttr); } else { cartItem.AttributesXml = await _attributeUtilities.InsertHomeDeliveryAttributeAsync( await _productService.GetProductByIdAsync(cartItem.ProductId), cartItem.AttributesXml); } await _shoppingCartService.UpdateShoppingCartItemAsync(await _workContext.GetCurrentCustomerAsync(), cartItem.Id, cartItem.AttributesXml, cartItem.CustomerEnteredPrice, null, null, cartItem.Quantity, false); } } return(Json(await _shopService.GetShopByIdAsync(int.Parse(shopId)))); }
public override async Task <IActionResult> AddProductToCart_Catalog(int productId, int shoppingCartTypeId, int quantity, bool forceredirection = false) { var cartType = (ShoppingCartType)shoppingCartTypeId; var product = await _productService.GetProductByIdAsync(productId); if (product == null) { //no product found return(Json(new { success = false, message = "No product found with the specified ID" })); } //we can add only simple products if (product.ProductType != ProductType.SimpleProduct) { return(Json(new { redirect = Url.RouteUrl("Product", new { SeName = await _urlRecordService.GetSeNameAsync(product) }) })); } //products with "minimum order quantity" more than a specified qty if (product.OrderMinimumQuantity > quantity) { //we cannot add to the cart such products from category pages //it can confuse customers. That's why we redirect customers to the product details page return(Json(new { redirect = Url.RouteUrl("Product", new { SeName = await _urlRecordService.GetSeNameAsync(product) }) })); } if (product.CustomerEntersPrice) { //cannot be added to the cart (requires a customer to enter price) return(Json(new { redirect = Url.RouteUrl("Product", new { SeName = await _urlRecordService.GetSeNameAsync(product) }) })); } if (product.IsRental) { //rental products require start/end dates to be entered return(Json(new { redirect = Url.RouteUrl("Product", new { SeName = await _urlRecordService.GetSeNameAsync(product) }) })); } var allowedQuantities = _productService.ParseAllowedQuantities(product); if (allowedQuantities.Length > 0) { //cannot be added to the cart (requires a customer to select a quantity from dropdownlist) return(Json(new { redirect = Url.RouteUrl("Product", new { SeName = await _urlRecordService.GetSeNameAsync(product) }) })); } string attributes = ""; ProductAttributeMapping hdProductAttribute = null; var pams = await _productAttributeService.GetProductAttributeMappingsByProductIdAsync(product.Id); foreach (var pam in pams) { var pa = await _productAttributeService.GetProductAttributeByIdAsync(pam.ProductAttributeId); switch (pa.Name) { case "Home Delivery": hdProductAttribute = pam; break; } } // home delivery is default, so if it is home delivered, add the attribute no matter what if (hdProductAttribute != null) { attributes = await _attributeUtilities.InsertHomeDeliveryAttributeAsync(product, attributes); } //-------------------------------------END CUSTOM CODE------------------------------------------ //get standard warnings without attribute validations //first, try to find existing shopping cart item var cart = await _shoppingCartService.GetShoppingCartAsync(await _workContext.GetCurrentCustomerAsync(), cartType, (await _storeContext.GetCurrentStoreAsync()).Id); //-----------------------------MODIFIED THIS LINE to add "attributes----------------------------- var shoppingCartItem = await _shoppingCartService.FindShoppingCartItemInTheCartAsync(cart, cartType, product, attributes); //if we already have the same product in the cart, then use the total quantity to validate var quantityToValidate = shoppingCartItem != null ? shoppingCartItem.Quantity + quantity : quantity; var addToCartWarnings = await _shoppingCartService .GetShoppingCartItemWarningsAsync(await _workContext.GetCurrentCustomerAsync(), cartType, product, (await _storeContext.GetCurrentStoreAsync()).Id, string.Empty, decimal.Zero, null, null, quantityToValidate, false, shoppingCartItem?.Id ?? 0, true, false, false, false); if (addToCartWarnings.Any()) { //cannot be added to the cart //let's display standard warnings return(Json(new { success = false, message = addToCartWarnings.ToArray() })); } // ---------------------------------MODIFIED THIS LINE TO ADD ATTRIBUTES------------------------------ //now let's try adding product to the cart (now including product attribute validation, etc) addToCartWarnings = await _shoppingCartService.AddToCartAsync(customer : await _workContext.GetCurrentCustomerAsync(), product : product, shoppingCartType : cartType, storeId : (await _storeContext.GetCurrentStoreAsync()).Id, quantity : quantity, attributesXml : attributes); if (addToCartWarnings.Any()) { //cannot be added to the cart //but we do not display attribute and gift card warnings here. let's do it on the product details page return(Json(new { redirect = Url.RouteUrl("Product", new { SeName = await _urlRecordService.GetSeNameAsync(product) }) })); } //added to the cart/wishlist switch (cartType) { case ShoppingCartType.Wishlist: { //activity log await _customerActivityService.InsertActivityAsync("PublicStore.AddToWishlist", string.Format(await _localizationService.GetResourceAsync("ActivityLog.PublicStore.AddToWishlist"), product.Name), product); if (_shoppingCartSettings.DisplayWishlistAfterAddingProduct || forceredirection) { //redirect to the wishlist page return(Json(new { redirect = Url.RouteUrl("Wishlist") })); } //display notification message and update appropriate blocks var shoppingCarts = await _shoppingCartService.GetShoppingCartAsync(await _workContext.GetCurrentCustomerAsync(), ShoppingCartType.Wishlist, (await _storeContext.GetCurrentStoreAsync()).Id); var updatetopwishlistsectionhtml = string.Format(await _localizationService.GetResourceAsync("Wishlist.HeaderQuantity"), shoppingCarts.Sum(item => item.Quantity)); return(Json(new { success = true, message = string.Format(await _localizationService.GetResourceAsync("Products.ProductHasBeenAddedToTheWishlist.Link"), Url.RouteUrl("Wishlist")), updatetopwishlistsectionhtml })); } case ShoppingCartType.ShoppingCart: default: { //activity log await _customerActivityService.InsertActivityAsync("PublicStore.AddToShoppingCart", string.Format(await _localizationService.GetResourceAsync("ActivityLog.PublicStore.AddToShoppingCart"), product.Name), product); if (_shoppingCartSettings.DisplayCartAfterAddingProduct || forceredirection) { //redirect to the shopping cart page return(Json(new { redirect = Url.RouteUrl("ShoppingCart") })); } return(await SlideoutJson(product)); } } }
public override async Task MigrateShoppingCartAsync(Customer fromCustomer, Customer toCustomer, bool includeCouponCodes) { await base.MigrateShoppingCartAsync(fromCustomer, toCustomer, includeCouponCodes); if (fromCustomer.Id == toCustomer.Id) { return; } var fromCsm = _customerShopService.GetCurrentCustomerShopMapping(fromCustomer.Id); if (fromCsm == null) { //old customer has no pickup items to update, do nothing return; } //update tocustomer's shop mapping _customerShopService.InsertOrUpdateCustomerShop(toCustomer.Id, fromCsm.ShopId); var csm = _customerShopService.GetCurrentCustomerShopMapping(toCustomer.Id); Shop shop = await _shopService.GetShopByIdAsync(csm.ShopId); //used to merge together products that will now have the same attributes Dictionary <int, ShoppingCartItem> productIdToPickupSci = new Dictionary <int, ShoppingCartItem>(); Dictionary <int, ShoppingCartItem> productIdToDeliverySci = new Dictionary <int, ShoppingCartItem>(); List <ShoppingCartItem> toDelete = new List <ShoppingCartItem>(); //update all pickup items in the cart to current availability status foreach (ShoppingCartItem sci in (await GetShoppingCartAsync(toCustomer)).Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart)) { ProductAttributeMapping pickupAttribute = await _attributeUtilities.GetPickupAttributeMappingAsync(sci.AttributesXml); if (pickupAttribute != null) { //if we already have an existing shoppingcart item for this product update its quantity if (productIdToPickupSci.ContainsKey(sci.ProductId)) { var existingSci = productIdToPickupSci[sci.ProductId]; await base.UpdateShoppingCartItemAsync(toCustomer, existingSci.Id, existingSci.AttributesXml, existingSci.CustomerEnteredPrice, null, null, existingSci.Quantity + sci.Quantity, false); toDelete.Add(sci); } else { // check if product is available at the selected store StockResponse stockResponse = await _backendStockService.GetApiStockAsync(sci.ProductId); bool available = false; if (stockResponse != null) { available = stockResponse.ProductStocks.Where(ps => ps.Available && ps.Shop.Id == csm.ShopId).Any(); } //if available clean and re add the pickup attribute if (available) { string removedAttr = _productAttributeParser.RemoveProductAttribute(sci.AttributesXml, pickupAttribute); sci.AttributesXml = await _attributeUtilities.InsertPickupAttributeAsync(await _productService.GetProductByIdAsync(sci.ProductId), stockResponse, removedAttr, shop); productIdToPickupSci[sci.ProductId] = sci; } else { //else we switch it to home delivery //merge home delivery if it exists if (productIdToDeliverySci.ContainsKey(sci.ProductId)) { var existingSci = productIdToDeliverySci[sci.ProductId]; await base.UpdateShoppingCartItemAsync(toCustomer, existingSci.Id, existingSci.AttributesXml, existingSci.CustomerEnteredPrice, null, null, existingSci.Quantity + sci.Quantity, false); toDelete.Add(sci); continue; } else { //else replace the pickup attribute with home delivery sci.AttributesXml = await _attributeUtilities.InsertHomeDeliveryAttributeAsync( await _productService.GetProductByIdAsync(sci.ProductId), sci.AttributesXml ); productIdToDeliverySci[sci.ProductId] = sci; } } //update the sci with new attributes await base.UpdateShoppingCartItemAsync(toCustomer, sci.Id, sci.AttributesXml, sci.CustomerEnteredPrice, null, null, sci.Quantity, false); } } else { //if not a pickup item, keep track for later merging productIdToDeliverySci[sci.ProductId] = sci; } } for (int i = 0; i < toDelete.Count; ++i) { await base.DeleteShoppingCartItemAsync(toDelete[i]); } }