示例#1
0
        public ActionResult UpdateProductDetails(int productId, string itemType, int bundleItemId, FormCollection form)
        {
            int    quantity          = 1;
            int    galleryStartIndex = -1;
            string galleryHtml       = null;
            string dynamicThumbUrl   = null;
            bool   isAssociated      = itemType.IsCaseInsensitiveEqual("associateditem");
            var    pictureModel      = new ProductDetailsPictureModel();
            var    m       = new ProductDetailsModel();
            var    product = _productService.GetProductById(productId);
            var    bItem   = _productService.GetBundleItemById(bundleItemId);
            IList <ProductBundleItemData> bundleItems = null;
            ProductBundleItemData         bundleItem  = (bItem == null ? null : new ProductBundleItemData(bItem));

            // quantity required for tier prices
            string quantityKey = form.AllKeys.FirstOrDefault(k => k.EndsWith("EnteredQuantity"));

            if (quantityKey.HasValue())
            {
                int.TryParse(form[quantityKey], out quantity);
            }

            if (product.ProductType == ProductType.BundledProduct && product.BundlePerItemPricing)
            {
                bundleItems = _productService.GetBundleItems(product.Id);
                if (form.Count > 0)
                {
                    foreach (var itemData in bundleItems)
                    {
                        var tempModel = _helper.PrepareProductDetailsPageModel(itemData.Item.Product, false, itemData, null, form);
                    }
                }
            }

            // get merged model data
            _helper.PrepareProductDetailModel(m, product, isAssociated, bundleItem, bundleItems, form, quantity);

            if (bundleItem != null)                     // update bundle item thumbnail
            {
                if (!bundleItem.Item.HideThumbnail)
                {
                    var picture = m.GetAssignedPicture(_pictureService, null, bundleItem.Item.ProductId);
                    dynamicThumbUrl = _pictureService.GetPictureUrl(picture, _mediaSettings.BundledProductPictureSize, false);
                }
            }
            else if (isAssociated)                      // update associated product thumbnail
            {
                var picture = m.GetAssignedPicture(_pictureService, null, productId);
                dynamicThumbUrl = _pictureService.GetPictureUrl(picture, _mediaSettings.AssociatedProductPictureSize, false);
            }
            else if (product.ProductType != ProductType.BundledProduct)                         // update image gallery
            {
                var pictures = _pictureService.GetPicturesByProductId(productId);

                if (pictures.Count <= _catalogSettings.DisplayAllImagesNumber)                  // all pictures rendered... only index is required
                {
                    var picture = m.GetAssignedPicture(_pictureService, pictures);
                    galleryStartIndex = (picture == null ? 0 : pictures.IndexOf(picture));
                }
                else
                {
                    var allCombinationImageIds = new List <int>();

                    _productAttributeService
                    .GetAllProductVariantAttributeCombinations(product.Id)
                    .GetAllCombinationImageIds(allCombinationImageIds);

                    _helper.PrepareProductDetailsPictureModel(pictureModel, pictures, product.GetLocalized(x => x.Name), allCombinationImageIds,
                                                              false, bundleItem, m.CombinationSelected);

                    galleryStartIndex = pictureModel.GalleryStartIndex;
                    galleryHtml       = this.RenderPartialViewToString("_PictureGallery", pictureModel);
                }
            }

            #region data object
            object data = new
            {
                Delivery = new
                {
                    Id    = 0,
                    Name  = m.DeliveryTimeName,
                    Color = m.DeliveryTimeHexValue,
                    DisplayAccordingToStock = m.DisplayDeliveryTimeAccordingToStock
                },
                Measure = new
                {
                    Weight = new { Value = m.WeightValue, Text = m.Weight },
                    Height = new { Value = product.Height, Text = m.Height },
                    Width  = new { Value = product.Width, Text = m.Width },
                    Length = new { Value = product.Length, Text = m.Length }
                },
                Number = new
                {
                    Sku  = new { Value = m.Sku, Show = m.ShowSku },
                    Gtin = new { Value = m.Gtin, Show = m.ShowGtin },
                    Mpn  = new { Value = m.ManufacturerPartNumber, Show = m.ShowManufacturerPartNumber }
                },
                Price = new
                {
                    Base = new
                    {
                        Enabled = m.IsBasePriceEnabled,
                        Info    = m.BasePriceInfo
                    },
                    Old = new
                    {
                        Value = decimal.Zero,
                        Text  = m.ProductPrice.OldPrice
                    },
                    WithoutDiscount = new
                    {
                        Value = m.ProductPrice.PriceValue,
                        Text  = m.ProductPrice.Price
                    },
                    WithDiscount = new
                    {
                        Value = m.ProductPrice.PriceWithDiscountValue,
                        Text  = m.ProductPrice.PriceWithDiscount
                    }
                },
                Stock = new
                {
                    Quantity     = new { Value = product.StockQuantity, Show = product.DisplayStockQuantity },
                    Availability = new { Text = m.StockAvailability, Show = product.DisplayStockAvailability, Available = m.IsAvailable }
                },

                DynamicThumblUrl  = dynamicThumbUrl,
                GalleryStartIndex = galleryStartIndex,
                GalleryHtml       = galleryHtml
            };
            #endregion

            return(new JsonResult {
                Data = data
            });
        }
        public ActionResult UpdateProductDetails(int productId, string itemType, int bundleItemId, ProductVariantQuery query, FormCollection form)
        {
            int    quantity          = 1;
            int    galleryStartIndex = -1;
            string galleryHtml       = null;
            string dynamicThumbUrl   = null;
            var    isAssociated      = itemType.IsCaseInsensitiveEqual("associateditem");
            var    pictureModel      = new ProductDetailsPictureModel();
            var    m       = new ProductDetailsModel();
            var    product = _productService.GetProductById(productId);
            var    bItem   = _productService.GetBundleItemById(bundleItemId);
            IList <ProductBundleItemData> bundleItems = null;
            ProductBundleItemData         bundleItem  = (bItem == null ? null : new ProductBundleItemData(bItem));

            // Quantity required for tier prices.
            string quantityKey = form.AllKeys.FirstOrDefault(k => k.EndsWith("EnteredQuantity"));

            if (quantityKey.HasValue())
            {
                int.TryParse(form[quantityKey], out quantity);
            }

            if (product.ProductType == ProductType.BundledProduct && product.BundlePerItemPricing)
            {
                bundleItems = _productService.GetBundleItems(product.Id);
                if (query.Variants.Count > 0)
                {
                    // May add elements to query object if they are preselected by bundle item filter.
                    foreach (var itemData in bundleItems)
                    {
                        _helper.PrepareProductDetailsPageModel(itemData.Item.Product, query, false, itemData, null);
                    }
                }
            }

            // Get merged model data.
            _helper.PrepareProductDetailModel(m, product, query, isAssociated, bundleItem, bundleItems, quantity);

            if (bundleItem != null)
            {
                // Update bundle item thumbnail.
                if (!bundleItem.Item.HideThumbnail)
                {
                    var picture = m.GetAssignedPicture(_pictureService, null, bundleItem.Item.ProductId);
                    dynamicThumbUrl = _pictureService.GetUrl(picture, _mediaSettings.BundledProductPictureSize, false);
                }
            }
            else if (isAssociated)
            {
                // Update associated product thumbnail.
                var picture = m.GetAssignedPicture(_pictureService, null, productId);
                dynamicThumbUrl = _pictureService.GetUrl(picture, _mediaSettings.AssociatedProductPictureSize, false);
            }
            else if (product.ProductType != ProductType.BundledProduct)
            {
                // Update image gallery.
                var pictures = _pictureService.GetPicturesByProductId(productId);

                if (pictures.Count <= _catalogSettings.DisplayAllImagesNumber)
                {
                    // All pictures rendered... only index is required.
                    var picture = m.GetAssignedPicture(_pictureService, pictures);
                    galleryStartIndex = (picture == null ? 0 : pictures.IndexOf(picture));
                }
                else
                {
                    var allCombinationPictureIds = _productAttributeService.GetAllProductVariantAttributeCombinationPictureIds(product.Id);

                    _helper.PrepareProductDetailsPictureModel(
                        pictureModel,
                        pictures,
                        product.GetLocalized(x => x.Name),
                        allCombinationPictureIds,
                        false,
                        bundleItem,
                        m.SelectedCombination);

                    galleryStartIndex = pictureModel.GalleryStartIndex;
                    galleryHtml       = this.RenderPartialViewToString("Product.Picture", pictureModel);
                }
            }

            object partials = null;

            if (m.IsBundlePart)
            {
                partials = new
                {
                    BundleItemPrice = this.RenderPartialViewToString("Product.Offer.Price", m),
                    BundleItemStock = this.RenderPartialViewToString("Product.StockInfo", m)
                };
            }
            else
            {
                var dataDictAddToCart = new ViewDataDictionary();
                dataDictAddToCart.TemplateInfo.HtmlFieldPrefix = string.Format("addtocart_{0}", m.Id);

                decimal adjustment = decimal.Zero;
                decimal taxRate    = decimal.Zero;
                var     finalPriceWithDiscountBase = _taxService.GetProductPrice(product, product.Price, _services.WorkContext.CurrentCustomer, out taxRate);

                if (!_taxSettings.Value.PricesIncludeTax && _services.WorkContext.TaxDisplayType == TaxDisplayType.IncludingTax)
                {
                    adjustment = (m.ProductPrice.PriceValue - finalPriceWithDiscountBase) / (taxRate / 100 + 1);
                }
                else if (_taxSettings.Value.PricesIncludeTax && _services.WorkContext.TaxDisplayType == TaxDisplayType.ExcludingTax)
                {
                    adjustment = (m.ProductPrice.PriceValue - finalPriceWithDiscountBase) * (taxRate / 100 + 1);
                }
                else
                {
                    adjustment = m.ProductPrice.PriceValue - finalPriceWithDiscountBase;
                }

                partials = new
                {
                    Attrs        = this.RenderPartialViewToString("Product.Attrs", m),
                    Price        = this.RenderPartialViewToString("Product.Offer.Price", m),
                    Stock        = this.RenderPartialViewToString("Product.StockInfo", m),
                    OfferActions = this.RenderPartialViewToString("Product.Offer.Actions", m, dataDictAddToCart),
                    TierPrices   = this.RenderPartialViewToString("Product.TierPrices", _helper.CreateTierPriceModel(product, adjustment)),
                    BundlePrice  = product.ProductType == ProductType.BundledProduct ? this.RenderPartialViewToString("Product.Bundle.Price", m) : (string)null
                };
            }

            object data = new
            {
                Partials          = partials,
                DynamicThumblUrl  = dynamicThumbUrl,
                GalleryStartIndex = galleryStartIndex,
                GalleryHtml       = galleryHtml
            };

            return(new JsonResult {
                Data = data
            });
        }
        public ActionResult UpdateProductDetails(int productId, string itemType, int bundleItemId, ProductVariantQuery query, FormCollection form)
        {
            int    quantity          = 1;
            int    galleryStartIndex = -1;
            string galleryHtml       = null;
            string dynamicThumbUrl   = null;
            bool   isAssociated      = itemType.IsCaseInsensitiveEqual("associateditem");
            var    pictureModel      = new ProductDetailsPictureModel();
            var    m       = new ProductDetailsModel();
            var    product = _productService.GetProductById(productId);
            var    bItem   = _productService.GetBundleItemById(bundleItemId);
            IList <ProductBundleItemData> bundleItems = null;
            ProductBundleItemData         bundleItem  = (bItem == null ? null : new ProductBundleItemData(bItem));

            var warnings   = new List <string>();
            var attributes = _productAttributeService.GetProductVariantAttributesByProductId(productId);

            var attributeXml = query.CreateSelectedAttributesXml(productId, 0, attributes, _productAttributeParser,
                                                                 _localizationService, _downloadService, _catalogSettings, this.Request, warnings);

            var areAllAttributesForCombinationSelected = _shoppingCartService.AreAllAttributesForCombinationSelected(attributeXml, product);

            // quantity required for tier prices
            string quantityKey = form.AllKeys.FirstOrDefault(k => k.EndsWith("EnteredQuantity"));

            if (quantityKey.HasValue())
            {
                int.TryParse(form[quantityKey], out quantity);
            }

            if (product.ProductType == ProductType.BundledProduct && product.BundlePerItemPricing)
            {
                bundleItems = _productService.GetBundleItems(product.Id);
                if (query.Variants.Count > 0)
                {
                    // may add elements to query object if they are preselected by bundle item filter
                    foreach (var itemData in bundleItems)
                    {
                        _helper.PrepareProductDetailsPageModel(itemData.Item.Product, query, false, itemData, null);
                    }
                }
            }

            // get merged model data
            _helper.PrepareProductDetailModel(m, product, query, isAssociated, bundleItem, bundleItems, quantity);

            if (bundleItem != null)             // update bundle item thumbnail
            {
                if (!bundleItem.Item.HideThumbnail)
                {
                    var picture = m.GetAssignedPicture(_pictureService, null, bundleItem.Item.ProductId);
                    dynamicThumbUrl = _pictureService.GetPictureUrl(picture, _mediaSettings.BundledProductPictureSize, false);
                }
            }
            else if (isAssociated)             // update associated product thumbnail
            {
                var picture = m.GetAssignedPicture(_pictureService, null, productId);
                dynamicThumbUrl = _pictureService.GetPictureUrl(picture, _mediaSettings.AssociatedProductPictureSize, false);
            }
            else if (product.ProductType != ProductType.BundledProduct)                 // update image gallery
            {
                var pictures = _pictureService.GetPicturesByProductId(productId);

                if (pictures.Count <= _catalogSettings.DisplayAllImagesNumber)                  // all pictures rendered... only index is required
                {
                    var picture = m.GetAssignedPicture(_pictureService, pictures);
                    galleryStartIndex = (picture == null ? 0 : pictures.IndexOf(picture));
                }
                else
                {
                    var allCombinationPictureIds = _productAttributeService.GetAllProductVariantAttributeCombinationPictureIds(product.Id);

                    _helper.PrepareProductDetailsPictureModel(
                        pictureModel,
                        pictures,
                        product.GetLocalized(x => x.Name),
                        allCombinationPictureIds,
                        false,
                        bundleItem,
                        m.SelectedCombination);

                    galleryStartIndex = pictureModel.GalleryStartIndex;
                    galleryHtml       = this.RenderPartialViewToString("Product.Picture", pictureModel);
                }
            }

            object partials = null;

            if (m.IsBundlePart)
            {
                partials = new
                {
                    BundleItemPrice = this.RenderPartialViewToString("Product.Offer.Price", m),
                    BundleItemStock = this.RenderPartialViewToString("Product.StockInfo", m)
                };
            }
            else
            {
                partials = new
                {
                    Attrs       = this.RenderPartialViewToString("Product.Attrs", m),
                    Price       = this.RenderPartialViewToString("Product.Offer.Price", m),
                    Stock       = this.RenderPartialViewToString("Product.StockInfo", m),
                    BundlePrice = product.ProductType == ProductType.BundledProduct ? this.RenderPartialViewToString("Product.Bundle.Price", m) : (string)null
                };
            }

            object data = new
            {
                Partials          = partials,
                DynamicThumblUrl  = dynamicThumbUrl,
                GalleryStartIndex = galleryStartIndex,
                GalleryHtml       = galleryHtml
            };

            return(new JsonResult {
                Data = data
            });
        }