Exemplo n.º 1
0
        private async Task <Shop> GetCustomerShop()
        {
            var currentCustomer = await _workContext.GetCurrentCustomerAsync();

            CustomerShopMapping csm = _customerShopMappingRepository.Table
                                      .Where(c => c.CustomerId == currentCustomer.Id)
                                      .Select(c => c).FirstOrDefault();

            // get the customer shop mapping, get the latest information about the store customer selected
            return(await _shopService.GetShopByIdAsync(csm.ShopId));
        }
Exemplo n.º 2
0
        public void InsertOrUpdateCustomerShop(int customerId, int shopId)
        {
            CustomerShopMapping csm = GetCurrentCustomerShopMapping(customerId);

            if (csm == null)
            {
                csm            = new CustomerShopMapping();
                csm.CustomerId = customerId;
                csm.ShopId     = shopId;

                _customerShopMappingRepository.InsertAsync(csm);
            }
            else
            {
                csm.ShopId = shopId;
                _customerShopMappingRepository.UpdateAsync(csm);
            }
        }
Exemplo n.º 3
0
        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 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));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Change(int shoppingCartItemId)
        {
            ShoppingCartItem item = _shoppingCartItemRepository.Table
                                    .Where(i => i.Id == shoppingCartItemId)
                                    .Select(i => i).FirstOrDefault();

            CustomerShopMapping csm = _customerShopService.GetCurrentCustomerShopMapping((await _workContext.GetCurrentCustomerAsync()).Id);

            // no store selected yet
            if (csm == null)
            {
                // kick back to product page
                var product = await _productService.GetProductByIdAsync(item.ProductId);

                var seName = await _urlRecordService.GetSeNameAsync <Product>(product);

                string url = Url.RouteUrl("Product", new { SeName = seName });
                url += "?updatecartitemid=" + item.Id;
                return(Json(new FailResponse
                {
                    RedirectUrl = url
                }));
            }

            Shop currentShop = await _shopService.GetShopByIdAsync(csm.ShopId);

            bool isHomeDeliveryProduct = _productHomeDeliveryRepository.Table
                                         .Where(hdp => hdp.Product_Id == item.ProductId)
                                         .Select(hdp => hdp).Any();

            string attributes = item.AttributesXml;

            // get xml and check if it's home delivery or pickup in store
            var pickupAttributeMapping = await _attributeUtilities.GetPickupAttributeMappingAsync(attributes);

            // is currently pickup in store
            if (pickupAttributeMapping != null)
            {
                if (isHomeDeliveryProduct)
                {
                    // replace pickup with home delivery if the item is a home delivery product
                    var product = await _productService.GetProductByIdAsync(item.ProductId);

                    attributes = await _attributeUtilities.InsertHomeDeliveryAttributeAsync(product, attributes);
                }
                else
                {
                    // remove pickup attributemapping
                    attributes = await _attributeUtilities.RemovePickupAttributesAsync(attributes);
                }
            }
            // is currently home delivery
            else
            {
                // check if this item can be picked up at the current store
                StockResponse response = await _backendStockService.GetApiStockAsync(item.ProductId);

                bool itemAvailableAtStore = response.ProductStocks
                                            .Where(p => p.Shop.Id == csm.ShopId && p.Available).Any();

                if (itemAvailableAtStore)
                {
                    // replace home delivery/shipping with pickup
                    var product = await _productService.GetProductByIdAsync(item.ProductId);

                    attributes = await _attributeUtilities.InsertPickupAttributeAsync(product, response, attributes);
                }
                else
                {
                    // item is not available at the store. do not change, then display a message to customer
                    return(Json(new FailResponse
                    {
                        ErrorMessageRecipient = "#change_method_error_" + shoppingCartItemId,
                        ErrorMessage = "This item is not avaialble for pickup at " + currentShop.Name
                    }));
                }
            }

            var cart = await _shoppingCartService.GetShoppingCartAsync(
                await _workContext.GetCurrentCustomerAsync(),
                ShoppingCartType.ShoppingCart,
                (await _storeContext.GetCurrentStoreAsync()).Id);

            ShoppingCartItem sameCartItem = cart
                                            .Where(sci => sci.Id != item.Id && sci.ProductId == item.ProductId && sci.AttributesXml == attributes)
                                            .Select(sci => sci).FirstOrDefault();

            if (sameCartItem == null)
            {
                await _shoppingCartService.UpdateShoppingCartItemAsync(await _workContext.GetCurrentCustomerAsync(), item.Id, attributes, item.CustomerEnteredPrice, null, null, item.Quantity, true);
            }
            else
            {
                await _shoppingCartService.UpdateShoppingCartItemAsync(await _workContext.GetCurrentCustomerAsync(), sameCartItem.Id,
                                                                       sameCartItem.AttributesXml, sameCartItem.CustomerEnteredPrice, null, null, item.Quantity + sameCartItem.Quantity, true);

                await _shoppingCartService.DeleteShoppingCartItemAsync(item);
            }

            return(new EmptyResult());
        }