public async Task <PickStoreModel> InitializePickStoreModelAsync() { // initialize model for the view PickStoreModel model = new PickStoreModel(); // get the store that the customer selected previously if selected at all CustomerShopMapping currentCustomerShopMapping = _customerShopService.GetCurrentCustomerShopMapping((await _workContext.GetCurrentCustomerAsync()).Id); var shoppingCartItems = await _shoppingCartService.GetShoppingCartAsync(await _workContext.GetCurrentCustomerAsync()); // if selected before, add it to the model, else -1 if (currentCustomerShopMapping != null) { model.SelectedShop = await _shopService.GetShopByIdAsync(currentCustomerShopMapping.ShopId); } model.PickupInStoreText = _pickupInStoreSettings.PickupInStoreText; model.GoogleMapsAPIKey = _storeLocatorSettings.GoogleApiKey; return(model); }
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]); } }