Exemplo n.º 1
0
        public ActionResult GetUpdatedCrossSell(string viewName, string promotionId, string emptyCartPromotionId, string title, int maxNumberOfProducts)
        {
            var cart = _cartApi.GetCartAsync().Result;
            CrossSellViewModel vm = null;
            var lineItem          = (cart.LineItems == null || cart.LineItems.LineItem == null) ? null : cart.LineItems.LineItem.FirstOrDefault();

            if (lineItem != null)
            {
                vm = _offersViewModelBuilder.GetCrossSellViewModelAsync(promotionId, lineItem.Product, _linkGenerator.GenerateShoppingCartLink()).Result;
            }

            // If there are no driving products, then try the empty cart promotion, as the CrossSellPartController does
            if (vm == null || vm.Offers.Count == 0)
            {
                vm = _offersViewModelBuilder.GetCrossSellViewModelAsync(!string.IsNullOrEmpty(emptyCartPromotionId) ? emptyCartPromotionId : promotionId, _linkGenerator.GenerateShoppingCartLink()).Result;
            }

            OffersViewModelBuilder.RemoveCartItemsFromOffers(vm, cart);

            if (vm == null)
            {
                vm = new CrossSellViewModel {
                    Offers = new List <CrossSellOfferViewModel>()
                }
            }
            ;

            vm.Title                = title;
            vm.PromotionId          = promotionId;
            vm.EmptyCartPromotionId = emptyCartPromotionId;
            vm.MaxNumberOfProducts  = maxNumberOfProducts;

            return(PartialView(viewName, vm));
        }
Exemplo n.º 2
0
        public override ActionResult Index()
        {
            CrossSellViewModel vm   = null;
            var promotionId         = CurrentItem.PromotionId;
            int?maxNumberOfProducts = 0;

            // We are on a shopping cart page. Try to build the cross-sell data with the items in the cart.
            string emptyCartPromotionId = null;

            if (CurrentItem != null)
            {
                emptyCartPromotionId = CurrentItem.EmptyCartPromotionId;
                maxNumberOfProducts  = CurrentItem.MaxNumberOfProducts;
            }

            //todo: Get the cart from session??
            var cartViewModel = WebSession.Current.Get <ShoppingCartViewModel>(WebSession.ShoppingCartSlot);
            var cart          = cartViewModel == null?_cartApi.GetCartAsync().Result : cartViewModel.Cart;

            if (cart.LineItems.LineItem != null)
            {
                foreach (var li in cart.LineItems.LineItem)
                {
                    if (li.Product != null)
                    {
                        vm = _offersViewModelBuilder.GetCrossSellViewModelAsync(promotionId, li.Product, _linkGenerator.GenerateShoppingCartLink()).Result;
                    }
                    if (vm != null)
                    {
                        break;
                    }
                }
            }

            // If nothing is in the cart, or if no Offers were returned for this product, try to use the default offers.
            if (vm == null || vm.Offers == null || vm.Offers.Count == 0)
            {
                vm = _offersViewModelBuilder.GetCrossSellViewModelAsync(promotionId, _linkGenerator.GenerateShoppingCartLink()).Result;
            }

            // Set the empty cart promotion Id, which is used by an Ajax call to the ShoppingCartPart.GetUpdatedCrossSell()
            if (vm != null)
            {
                vm.EmptyCartPromotionId = emptyCartPromotionId;
            }

            OffersViewModelBuilder.RemoveCartItemsFromOffers(vm, cart);

            if (vm == null)
            {
                vm = new CrossSellViewModel {
                    Offers = new List <CrossSellOfferViewModel>()
                }
            }
            ;

            vm.Name                = CurrentItem.Name;
            vm.Title               = CurrentItem.Title;
            vm.PromotionId         = promotionId;
            vm.MaxNumberOfProducts = maxNumberOfProducts ?? 0;
            return(PartialView(vm));
        }
    }
Exemplo n.º 3
0
        public override ActionResult Index()
        {
            CrossSellViewModel vm   = null;
            var promotionId         = CurrentItem.PromotionId;
            int?maxNumberOfProducts = 0;

            if (CurrentItem is CrossSellInterstitialPart)
            {
                var siteRoot = CmsFinder.FindSitePageFromSiteId(WebSession.Current.SiteId);
                if (siteRoot != null && !string.IsNullOrEmpty(siteRoot.PromotionId))
                {
                    promotionId = siteRoot.PromotionId;
                }
            }

            if (CurrentPage is ShoppingCartPage)
            {
                // We are on a shopping cart page. Try to build the cross-sell data with the items in the cart.
                string emptyCartPromotionId = null;
                var    part = CurrentItem as CandyRackPart;
                if (part != null)
                {
                    emptyCartPromotionId = part.EmptyCartPromotionId;
                    maxNumberOfProducts  = part.MaxNumberOfProducts;
                }

                //todo: Get the cart from session??
                var cartViewModel = WebSession.Current.Get <ShoppingCartViewModel>(WebSession.ShoppingCartSlot);
                var cart          = cartViewModel == null?_cartApi.GetCartAsync().Result : cartViewModel.Cart;

                if (cart.LineItems.LineItem != null)
                {
                    foreach (var li in cart.LineItems.LineItem)
                    {
                        if (li.Product != null)
                        {
                            vm = _offersViewModelBuilder.GetCrossSellViewModelAsync(promotionId, li.Product, _linkGenerator.GenerateShoppingCartLink()).Result;
                        }
                        if (vm != null)
                        {
                            break;
                        }
                    }
                }

                // If nothing is in the cart, or if no Offers were returned for this product, try to use the default offers.
                if (vm == null || vm.Offers == null || vm.Offers.Count == 0)
                {
                    vm = _offersViewModelBuilder.GetCrossSellViewModelAsync(promotionId, _linkGenerator.GenerateShoppingCartLink()).Result;
                }

                // Set the empty cart promotion Id, which is used by an Ajax call to the ShoppingCartPart.GetUpdatedCrossSell()
                if (vm != null)
                {
                    vm.EmptyCartPromotionId = emptyCartPromotionId;
                }

                OffersViewModelBuilder.RemoveCartItemsFromOffers(vm, cart);
            }
            else if (CurrentPage is ProductPage || CurrentPage is ShoppingCartInterstitialPage)
            {
                ProductDetailPageViewModel product;
                if (WebSession.Current.TryGet(WebSession.CurrentProductSlot, out product))
                {
                    if (product != null)
                    {
                        var crossSellProduct = product.Product;
                        var scip             = CurrentPage as ShoppingCartInterstitialPage;
                        if (scip != null && scip.ProductID != null)
                        {
                            var scipPid = long.Parse(scip.ProductID);
                            crossSellProduct = product.Product.Variations.Product.FirstOrDefault(p => p.Id == scipPid);
                        }


                        vm = _offersViewModelBuilder.GetCrossSellViewModelAsync(promotionId, crossSellProduct, _linkGenerator.GenerateShoppingCartLink()).Result;
                        maxNumberOfProducts = 999;
                    }
                }
            }

            if (vm == null)
            {
                vm = new CrossSellViewModel {
                    Offers = new List <CrossSellOfferViewModel>()
                }
            }
            ;

            vm.Name                = CurrentItem.Name;
            vm.Title               = CurrentItem.Title;
            vm.PromotionId         = promotionId;
            vm.MaxNumberOfProducts = maxNumberOfProducts ?? 0;
            return(PartialView(vm));
        }
    }
        // virtual for testing only!
        protected virtual ActionResult InternalAddToCart(AddProductModel[] addProductModels, bool skipInterstitial = false, int cpeCode = 0, int scsCode = 0)
        {
            if (addProductModels != null)
            {
                var errorMessages = new List <string>();

                // put the product(s) in the cart
                foreach (var addProductModel in addProductModels)
                {
                    try
                    {
                        var cart = CartApi.AddProductToCartAsync(Convert.ToInt64(addProductModel.ProductId), Convert.ToInt32(addProductModel.Quantity),
                                                                 Convert.ToInt64(addProductModel.OfferId)).Result;
                        WebSession.Current.SetPersistentProperty(WebSession.ShoppingCartCount, cart.TotalItemsInCart.ToString(CultureInfo.InvariantCulture));
                    }
                    catch (Exception exc)
                    {
                        var tempErrorMessages   = new List <string>();
                        var shopperApiException = exc as ShopperApiException;
                        if (shopperApiException != null)
                        {
                            switch (shopperApiException.ShopperApiError.Code)
                            {
                            case "inventory-unavailable-error":
                                tempErrorMessages.Add(Res.ShoppingCart_AddProductOutOfStock);
                                break;

                            default:
                                tempErrorMessages.Add(Res.ShoppingCart_AddProductFailed);
                                break;
                            }
                        }
                        else
                        {
                            tempErrorMessages.Add(Res.ShoppingCart_AddProductFailed);
                        }
                        errorMessages.AddRange(tempErrorMessages);
                    }
                }
                if (errorMessages.Any())
                {
                    var model = ShoppingCartViewModelBuilder.GetShoppingCartViewModelAsync().Result;
                    model.ErrorMessages = errorMessages.ToArray();
                    WebSessionSet(WebSession.ShoppingCartSlot, model);
                    return(Index());
                }

                if (addProductModels.Length == 1 && !skipInterstitial)
                {
                    var productId        = int.Parse(addProductModels[0].ProductId);
                    var interstitialLink = LinkGenerator.GenerateInterstitialLink(productId);
                    if (!String.IsNullOrEmpty(interstitialLink))
                    {
                        return(Redirect(interstitialLink));
                    }

                    var siteRoot = CmsFinder.FindSitePageFromSiteId(WebSession.Current.SiteId);
                    if (siteRoot != null && !String.IsNullOrEmpty(siteRoot.PromotionId))
                    {
                        var offers = OffersViewModelBuilder.GetCrossSellViewModelAsync(siteRoot.PromotionId,
                                                                                       CatalogApi.GetProductUri(productId), LinkGenerator.GenerateShoppingCartLink()).Result;
                        if (offers != null && offers.Offers.Count > 0)
                        {
                            interstitialLink = LinkGenerator.GenerateInterstitialLink();
                            if (!String.IsNullOrEmpty(interstitialLink))
                            {
                                return(Redirect(interstitialLink + "/" + productId));
                            }
                        }
                    }
                }
            }
            var cartLink = LinkGenerator.GenerateShoppingCartLink();

            if (scsCode != 0)
            {
                cartLink +=
                    (cartLink.LastIndexOf('?') == -1 ? "?" : "&") + ParamScsErrorCode + "=" + scsCode;
            }
            return(Redirect(cartLink));
        }