Пример #1
0
        private bool AddOrderItemsToCart(ICollection <OrderItemDto> orderItems, Customer customer, int storeId)
        {
            bool shouldReturnError = false;

            foreach (var orderItem in orderItems)
            {
                Product product = _productService.GetProductById(orderItem.ProductId.Value);

                if (!product.IsRental)
                {
                    orderItem.RentalStartDateUtc = null;
                    orderItem.RentalEndDateUtc   = null;
                }

                string attributesXml = _productAttributeConverter.ConvertToXml(orderItem.Attributes, product.Id);

                IList <string> errors = _shoppingCartService.AddToCart(customer, product,
                                                                       ShoppingCartType.ShoppingCart, storeId, attributesXml,
                                                                       0M, orderItem.RentalStartDateUtc, orderItem.RentalEndDateUtc,
                                                                       orderItem.Quantity ?? 1);

                if (errors.Count > 0)
                {
                    foreach (var error in errors)
                    {
                        ModelState.AddModelError("order", error);
                    }

                    shouldReturnError = true;
                }
            }

            return(shouldReturnError);
        }
Пример #2
0
        public IActionResult CreateShoppingCartItem([ModelBinder(typeof(JsonModelBinder <ShoppingCartItemDto>))] Delta <ShoppingCartItemDto> shoppingCartItemDelta)
        {
            // Here we display the errors if the validation has failed at some point.
            if (!ModelState.IsValid)
            {
                return(Error());
            }

            ShoppingCartItem newShoppingCartItem = _factory.Initialize();

            shoppingCartItemDelta.Merge(newShoppingCartItem);

            // We know that the product id and customer id will be provided because they are required by the validator.
            // TODO: validate
            Product product = _productService.GetProductById(newShoppingCartItem.ProductId);

            if (product == null)
            {
                return(Error(HttpStatusCode.NotFound, "product", "not found"));
            }

            Customer customer = _customerService.GetCustomerById(newShoppingCartItem.CustomerId);

            if (customer == null)
            {
                return(Error(HttpStatusCode.NotFound, "customer", "not found"));
            }

            ShoppingCartType shoppingCartType = (ShoppingCartType)Enum.Parse(typeof(ShoppingCartType), shoppingCartItemDelta.Dto.ShoppingCartType);

            if (!product.IsRental)
            {
                newShoppingCartItem.RentalStartDateUtc = null;
                newShoppingCartItem.RentalEndDateUtc   = null;
            }

            string attributesXml = _productAttributeConverter.ConvertToXml(shoppingCartItemDelta.Dto.Attributes, product.Id);

            int currentStoreId = _storeContext.CurrentStore.Id;

            IList <string> warnings = _shoppingCartService.AddToCart(customer, product, shoppingCartType, currentStoreId, attributesXml, 0M,
                                                                     newShoppingCartItem.RentalStartDateUtc, newShoppingCartItem.RentalEndDateUtc,
                                                                     shoppingCartItemDelta.Dto.Quantity ?? 1);

            if (warnings.Count > 0)
            {
                foreach (var warning in warnings)
                {
                    ModelState.AddModelError("shopping cart item", warning);
                }

                return(Error(HttpStatusCode.BadRequest));
            }
            else
            {
                // the newly added shopping cart item should be the last one
                newShoppingCartItem = customer.ShoppingCartItems.LastOrDefault();
            }

            // Preparing the result dto of the new product category mapping
            ShoppingCartItemDto newShoppingCartItemDto = _dtoHelper.PrepareShoppingCartItemDTO(newShoppingCartItem);

            var shoppingCartsRootObject = new ShoppingCartItemsRootObject();

            shoppingCartsRootObject.ShoppingCartItems.Add(newShoppingCartItemDto);

            var json = _jsonFieldsSerializer.Serialize(shoppingCartsRootObject, string.Empty);

            return(new RawJsonActionResult(json));
        }