Example #1
0
        public ActionResult AskQuestionAjax(int id, ProductVariantQuery query)
        {
            // Get attributeXml from product variant query
            if (query != null && id > 0)
            {
                var attributes    = _productAttributeService.GetProductVariantAttributesByProductId(id);
                var warnings      = new List <string>();
                var attributesXml = query.CreateSelectedAttributesXml(id, 0, attributes, _productAttributeParser,
                                                                      _localizationService, _downloadService, _catalogSettings, null, warnings);

                if (attributesXml.HasValue())
                {
                    TempData.Add("AskQuestionAttributesXml-" + id, attributesXml);
                }
            }

            return(new JsonResult
            {
                Data = new { redirect = Url.Action("AskQuestion", new { id }) },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            });
        }
Example #2
0
        protected virtual decimal GetPreselectedPrice(
            Product product,
            Customer customer,
            PriceCalculationContext context,
            ProductBundleItemData bundleItem,
            IEnumerable <ProductBundleItemData> bundleItems)
        {
            var taxRate = decimal.Zero;
            var attributesTotalPriceBase       = decimal.Zero;
            var preSelectedPriceAdjustmentBase = decimal.Zero;
            var isBundle            = (product.ProductType == ProductType.BundledProduct);
            var isBundleItemPricing = (bundleItem != null && bundleItem.Item.BundleProduct.BundlePerItemPricing);
            var isBundlePricing     = (bundleItem != null && !bundleItem.Item.BundleProduct.BundlePerItemPricing);
            var bundleItemId        = (bundleItem == null ? 0 : bundleItem.Item.Id);

            var query = new ProductVariantQuery();
            var selectedAttributeValues = new List <ProductVariantAttributeValue>();
            var attributes = context.Attributes.GetOrLoad(product.Id);

            // 1. Fill query with initially selected attributes.
            foreach (var attribute in attributes.Where(x => x.ProductVariantAttributeValues.Count > 0 && x.ShouldHaveValues()))
            {
                int preSelectedValueId = 0;
                ProductVariantAttributeValue defaultValue = null;
                var selectedValueIds = new List <int>();
                var pvaValues        = attribute.ProductVariantAttributeValues;

                foreach (var pvaValue in pvaValues)
                {
                    ProductBundleItemAttributeFilter attributeFilter = null;

                    if (bundleItem.FilterOut(pvaValue, out attributeFilter))
                    {
                        continue;
                    }

                    if (preSelectedValueId == 0 && attributeFilter != null && attributeFilter.IsPreSelected)
                    {
                        preSelectedValueId = attributeFilter.AttributeValueId;
                    }

                    if (!isBundlePricing && pvaValue.IsPreSelected)
                    {
                        decimal attributeValuePriceAdjustment = GetProductVariantAttributeValuePriceAdjustment(pvaValue);
                        decimal priceAdjustmentBase           = _taxService.GetProductPrice(product, attributeValuePriceAdjustment, out taxRate);

                        preSelectedPriceAdjustmentBase = decimal.Add(preSelectedPriceAdjustmentBase, priceAdjustmentBase);
                    }
                }

                // Value pre-selected by a bundle item filter discards the default pre-selection.
                if (preSelectedValueId != 0 && (defaultValue = pvaValues.FirstOrDefault(x => x.Id == preSelectedValueId)) != null)
                {
                    //defaultValue.IsPreSelected = true;
                    selectedAttributeValues.Add(defaultValue);
                    query.AddVariant(new ProductVariantQueryItem(defaultValue.Id.ToString())
                    {
                        ProductId          = product.Id,
                        BundleItemId       = bundleItemId,
                        AttributeId        = attribute.ProductAttributeId,
                        VariantAttributeId = attribute.Id,
                        Alias      = attribute.ProductAttribute.Alias,
                        ValueAlias = defaultValue.Alias
                    });
                }
                else
                {
                    foreach (var value in pvaValues.Where(x => x.IsPreSelected))
                    {
                        selectedAttributeValues.Add(value);
                        query.AddVariant(new ProductVariantQueryItem(value.Id.ToString())
                        {
                            ProductId          = product.Id,
                            BundleItemId       = bundleItemId,
                            AttributeId        = attribute.ProductAttributeId,
                            VariantAttributeId = attribute.Id,
                            Alias      = attribute.ProductAttribute.Alias,
                            ValueAlias = value.Alias
                        });
                    }
                }
            }

            // 2. Find attribute combination for selected attributes and merge it.
            if (!isBundle && query.Variants.Count > 0)
            {
                var attributeXml = query.CreateSelectedAttributesXml(product.Id, bundleItemId, attributes, _productAttributeParser, _services.Localization,
                                                                     _downloadService, _catalogSettings, _httpRequestBase, new List <string>());

                var combinations = context.AttributeCombinations.GetOrLoad(product.Id);

                var selectedCombination = combinations.FirstOrDefault(x => _productAttributeParser.AreProductAttributesEqual(x.AttributesXml, attributeXml));

                if (selectedCombination != null && selectedCombination.IsActive && selectedCombination.Price.HasValue)
                {
                    product.MergedDataValues = new Dictionary <string, object> {
                        { "Price", selectedCombination.Price.Value }
                    };

                    if (selectedCombination.BasePriceAmount.HasValue)
                    {
                        product.MergedDataValues.Add("BasePriceAmount", selectedCombination.BasePriceAmount.Value);
                    }

                    if (selectedCombination.BasePriceBaseAmount.HasValue)
                    {
                        product.MergedDataValues.Add("BasePriceBaseAmount", selectedCombination.BasePriceBaseAmount.Value);
                    }
                }
            }

            if (_catalogSettings.EnableDynamicPriceUpdate && !isBundlePricing)
            {
                if (selectedAttributeValues.Count > 0)
                {
                    selectedAttributeValues.Each(x => attributesTotalPriceBase += GetProductVariantAttributeValuePriceAdjustment(x));
                }
                else
                {
                    attributesTotalPriceBase = preSelectedPriceAdjustmentBase;
                }
            }

            if (bundleItem != null)
            {
                bundleItem.AdditionalCharge = attributesTotalPriceBase;
            }

            var result = GetFinalPrice(product, bundleItems, customer, attributesTotalPriceBase, true, 1, bundleItem, context);

            return(result);
        }
        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
            });
        }