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 async Task <IActionResult> AddProductToCart_Pickup( int productId, int shoppingCartTypeId, IFormCollection form ) { var product = await _productService.GetProductByIdAsync(productId); if (product == null) { return(Json(new { redirect = Url.RouteUrl("Homepage") })); } //we can add only simple products if (product.ProductType != ProductType.SimpleProduct) { return(Json(new { success = false, message = "Only simple products could be added to the cart" })); } //-------------------------------------CUSTOM CODE------------------------------------------ // force customer to select a store if store not already selected //TODO: This could be cached, would have to be cleared when the order is complete var customerId = (await _workContext.GetCurrentCustomerAsync()).Id; CustomerShopMapping csm = _customerShopMappingRepository.Table .Where(c => c.CustomerId == customerId) .Select(c => c).FirstOrDefault(); if (csm == null) { // return & force them to select a store return(Json(new { success = false, message = "Select a store to pick the item up" })); } // check if item is available StockResponse stockResponse = await _backendStockService.GetApiStockAsync(product.Id); if (stockResponse == null) { bool availableAtStore = stockResponse.ProductStocks .Where(ps => ps.Shop.Id == csm.ShopId) .Select(ps => ps.Available).FirstOrDefault(); if (!availableAtStore) { return(Json(new { success = false, message = "This item is not available at this store" })); } } //----------------------------------END CUSTOM CODE------------------------------------------ //update existing shopping cart item var updatecartitemid = 0; foreach (var formKey in form.Keys) { if (formKey.Equals($"addtocart_{productId}.UpdatedShoppingCartItemId", StringComparison.InvariantCultureIgnoreCase)) { int.TryParse(form[formKey], out updatecartitemid); break; } } ShoppingCartItem updatecartitem = null; if (_shoppingCartSettings.AllowCartItemEditing && updatecartitemid > 0) { //search with the same cart type as specified var cart = await _shoppingCartService.GetShoppingCartAsync(await _workContext.GetCurrentCustomerAsync(), (ShoppingCartType)shoppingCartTypeId, (await _storeContext.GetCurrentStoreAsync()).Id); updatecartitem = cart.FirstOrDefault(x => x.Id == updatecartitemid); //not found? let's ignore it. in this case we'll add a new item //if (updatecartitem == null) //{ // return Json(new // { // success = false, // message = "No shopping cart item found to update" // }); //} //is it this product? if (updatecartitem != null && product.Id != updatecartitem.ProductId) { return(Json(new { success = false, message = "This product does not match a passed shopping cart item identifier" })); } } var addToCartWarnings = new List <string>(); //customer entered price var customerEnteredPriceConverted = await _productAttributeParser.ParseCustomerEnteredPriceAsync(product, form); //entered quantity var quantity = _productAttributeParser.ParseEnteredQuantity(product, form); //product and gift card attributes var attributes = await _productAttributeParser.ParseProductAttributesAsync(product, form, addToCartWarnings); //---------------------------------------------START CUSTOM CODE----------------------------------------------------- if (stockResponse != null) { attributes = await _attributeUtilities.InsertPickupAttributeAsync(product, stockResponse, attributes); } //----------------------------------------------END CUSTOM CODE------------------------------------------------------ //rental attributes _productAttributeParser.ParseRentalDates(product, form, out var rentalStartDate, out var rentalEndDate); var cartType = updatecartitem == null ? (ShoppingCartType)shoppingCartTypeId : //if the item to update is found, then we ignore the specified "shoppingCartTypeId" parameter updatecartitem.ShoppingCartType; await SaveItemAsync(updatecartitem, addToCartWarnings, product, cartType, attributes, customerEnteredPriceConverted, rentalStartDate, rentalEndDate, quantity); //return result return(await GetProductToCartDetails(addToCartWarnings, cartType, 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]); } }