Esempio n. 1
0
        public async Task <JsonResult> AddToCart(RequestParamsToCart param) // only use Code
        {
            if (WishList.Cart == null)
            {
                _wishlist = new CartWithValidationIssues
                {
                    Cart             = _cartService.LoadOrCreateCart(_cartService.DefaultWishListName),
                    ValidationIssues = new Dictionary <ILineItem, List <ValidationIssue> >()
                };
            }

            // return 0 if the variant already exist in wishlist
            // return 1 if added susscessfully
            var result       = new AddToCartResult();
            var allLineItems = WishList.Cart.GetAllLineItems();
            var contentLink  = _referenceConverter.GetContentLink(param.Code);
            var message      = "";
            var productName  = "";
            var entryLink    = _referenceConverter.GetContentLink(param.Code);

            productName = _contentLoader.Get <EntryContentBase>(entryLink).DisplayName;

            if (_contentLoader.Get <EntryContentBase>(contentLink) is GenericBundle bundle) // Add bundle
            {
                var variantCodes = _contentLoader
                                   .GetItems(bundle.GetEntries(_relationRepository), _languageResolver.GetPreferredCulture())
                                   .OfType <VariationContent>()
                                   .Where(v => v.IsAvailableInCurrentMarket(_currentMarket) && !_filterPublished.ShouldFilter(v))
                                   .Select(x => x.Code);
                var allLineItemCodes = allLineItems.Select(x => x.Code);
                var allNewCodes      = variantCodes.Where(x => !allLineItemCodes.Contains(x));
                if (!allNewCodes.Any())
                {
                    return(Json(new ChangeCartJsonResult {
                        StatusCode = 0, Message = productName + " already exist in the wishlist."
                    }));
                }
                else
                {
                    foreach (var v in allNewCodes)
                    {
                        result = _cartService.AddToCart(WishList.Cart, new RequestParamsToCart {
                            Code = v, Quantity = 1, Store = "delivery", SelectedStore = ""
                        });
                        if (result.ValidationMessages.Count > 0)
                        {
                            message += string.Join("\n", result.ValidationMessages);
                        }
                    }
                }
            }
            else // Add variant
            {
                if (allLineItems.Any(item => item.Code.Equals(param.Code, StringComparison.OrdinalIgnoreCase)))
                {
                    return(Json(new ChangeCartJsonResult {
                        StatusCode = 0, Message = productName + " already exist in the wishlist."
                    }));
                }

                result = _cartService.AddToCart(WishList.Cart,
                                                new RequestParamsToCart {
                    Code = param.Code, Quantity = 1, Store = "delivery", SelectedStore = ""
                });
            }

            if (result.EntriesAddedToCart)
            {
                _orderRepository.Save(WishList.Cart);
                await _trackingService.TrackWishlist(HttpContext);

                return(Json(new ChangeCartJsonResult
                {
                    StatusCode = 1,
                    CountItems = (int)WishList.Cart.GetAllLineItems().Sum(x => x.Quantity),
                    Message = productName + " is added to the wishlist successfully.\n" + message
                }));
            }
            return(Json(new ChangeCartJsonResult {
                StatusCode = -1, Message = result.GetComposedValidationMessage()
            }));
        }
        public async Task <ActionResult> GetRecommendations(string widgetType, string name, string value = "", int numberOfRecs = 4)
        {
            if (string.IsNullOrEmpty(widgetType) || PageEditing.PageIsInEditMode)
            {
                return(new EmptyResult());
            }

            List <Recommendation> recommendations = null;
            TrackingResponseData  response;

            switch (widgetType)
            {
            case "Home":
                response = await _trackingService.TrackHome(ControllerContext.HttpContext);

                recommendations = response.GetRecommendations(_referenceConverter, RecommendationsExtensions.Home)
                                  .ToList();
                break;

            case "Basket":
                response = await _trackingService.TrackCart(ControllerContext.HttpContext, _cartService.LoadCart(_cartService.DefaultCartName, false).Cart);

                recommendations = response.GetRecommendations(_referenceConverter, RecommendationsExtensions.Basket)
                                  .ToList();
                break;

            case "Checkout":
                response = await _trackingService.TrackCheckout(ControllerContext.HttpContext);

                recommendations = response.GetRecommendations(_referenceConverter, "Checkout")
                                  .ToList();
                break;

            case "Wishlist":
                response = await _trackingService.TrackWishlist(ControllerContext.HttpContext);

                recommendations = response.GetRecommendations(_referenceConverter, "Wishlist")
                                  .ToList();
                break;

            case "Order":
                IPurchaseOrder order = null;
                if (PageEditing.PageIsInEditMode)
                {
                    break;
                }
                if (int.TryParse(ControllerContext.HttpContext.Request.QueryString["orderNumber"], out var orderNumber))
                {
                    order = _confirmationService.GetOrder(orderNumber);
                }
                if (order == null)
                {
                    break;
                }
                response = await _trackingService.TrackOrder(ControllerContext.HttpContext, order);

                recommendations = response.GetRecommendations(_referenceConverter, "orderWidget")
                                  .ToList();
                break;

            default:
                response = await _trackingService.TrackAttribute(ControllerContext.HttpContext, name, value);

                recommendations = response.GetRecommendations(_referenceConverter, "attributeWidget")
                                  .ToList();
                break;
            }

            if (recommendations == null)
            {
                return(new EmptyResult());
            }
            recommendations = recommendations.Take(numberOfRecs).ToList();

            return(PartialView("/Features/Recommendations/Index.cshtml", _productService.GetRecommendedProductTileViewModels(recommendations)));
        }