/// <summary>
        /// Initializes a new instance of the <see cref="ProductOptionWrapper"/> class.
        /// </summary>
        /// <param name="display">
        /// The display.
        /// </param>
        /// <param name="_parent">
        /// The parent content.
        /// </param>
        /// <param name="contentType">
        /// The content Type.
        /// </param>
        public ProductOptionWrapper(ProductOptionDisplay display, IPublishedContent _parent, PublishedContentType contentType = null)
        {
            _display     = display;
            _contentType = contentType;

            Initialize(_parent);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ProductOptionWrapper"/> class.
        /// </summary>
        /// <param name="display">
        /// The display.
        /// </param>
        /// <param name="_parent">
        /// The parent content.
        /// </param>
        /// <param name="contentType">
        /// The content Type.
        /// </param>
        /// <param name="usesContentTypeOverride">
        /// The uses Content Type Override.
        /// </param>
        public ProductOptionWrapper(ProductOptionDisplay display, IPublishedContent _parent, PublishedContentType contentType = null, bool usesContentTypeOverride = false)
        {
            _display                 = display;
            _contentType             = contentType;
            _usesContentTypeOverride = usesContentTypeOverride;

            Initialize(_parent);
        }
        public ProductOptionDisplay PostProductOption(ProductOptionDisplay option)
        {
            var productOption = option.ToProductOption(new ProductOption(option.Name));

            _productOptionService.Save(productOption);

            return(productOption.ToProductOptionDisplay(DetachedValuesConversionType.Editor));
        }
        public ProductOptionDisplay PutProductOption(ProductOptionDisplay option)
        {
            var destination = _productOptionService.GetByKey(option.Key);

            destination = option.ToProductOption(destination);
            _productOptionService.Save(destination);

            return(destination.ToProductOptionDisplay(DetachedValuesConversionType.Editor));
        }
        internal static void EnsureValueConversion(this ProductOptionDisplay display, DetachedValuesConversionType conversionType = DetachedValuesConversionType.Db)
        {
            if (display.DetachedContentTypeKey.Equals(Guid.Empty))
            {
                return;
            }

            var contentType = new Lazy <IContentType>(() => DetachedValuesConverter.Current.GetContentTypeByKey(display.DetachedContentTypeKey));

            foreach (var choice in display.Choices.Where(x => x.ValueConversion != conversionType))
            {
                if (contentType.Value != null)
                {
                    choice.EnsureValueConversion(contentType.Value, conversionType);
                }
            }
        }
Пример #6
0
        public IEnumerable <ProductOptionDisplay> FilterOptionChoices(Guid productKey, Guid productAttributeKey)
        {
            var product = _merchello.Query.Product.GetByKey(productKey);

            if (product == null)
            {
                var nullReference = new NullReferenceException("Product with key " + productKey + " returned null");
                LogHelper.Error <BazaarSiteApiController>("MerchelloHelper failed to retrieve product.", nullReference);
                throw nullReference;
            }

            // TODO move this to a service

            var returnOptions = new List <ProductOptionDisplay>();

            // this is the option the was just used in a selection
            var activeOption = product.ProductOptions.FirstOrDefault(po => po.Choices.Any(c => c.Key == productAttributeKey));

            if (activeOption == null)
            {
                return(returnOptions);
            }

            ProductVariantDisplay[] variants;

            // special case for a product with a single option
            // TODO clean this up
            if (1 == product.ProductOptions.Count())
            {
                variants = product.ProductVariants.Where(pv => pv.Available).ToArray();

                var addOption = new ProductOptionDisplay()
                {
                    SortOrder = activeOption.SortOrder,
                    Key       = activeOption.Key,
                    Name      = activeOption.Name
                };
                var optionChoices = new List <ProductAttributeDisplay>();

                foreach (var choice in activeOption.Choices)
                {
                    if (ValidateOptionChoice(variants, choice.Key))
                    {
                        optionChoices.Add(new ProductAttributeDisplay()
                        {
                            Key       = choice.Key,
                            Name      = choice.Name,
                            Sku       = choice.Sku,
                            OptionKey = choice.OptionKey,
                            SortOrder = choice.SortOrder
                        });
                    }
                }
                addOption.Choices = optionChoices;
                returnOptions.Add(addOption);
                return(returnOptions);
            }

            returnOptions.Add(activeOption);


            var otherOptions = product.ProductOptions.Where(x => !x.Key.Equals(activeOption.Key)).ToArray();

            variants = product.ProductVariants.Where(pv => pv.Available && pv.Attributes.Any(att => att.Key == productAttributeKey)).ToArray();

            foreach (var option in otherOptions)
            {
                var addOption = new ProductOptionDisplay()
                {
                    SortOrder = option.SortOrder,
                    Key       = option.Key,
                    Name      = option.Name
                };

                var optionChoices = new List <ProductAttributeDisplay>();

                foreach (var choice in option.Choices)
                {
                    if (ValidateOptionChoice(variants, choice.Key))
                    {
                        optionChoices.Add(new ProductAttributeDisplay()
                        {
                            Key       = choice.Key,
                            Name      = choice.Name,
                            Sku       = choice.Sku,
                            OptionKey = choice.OptionKey,
                            SortOrder = choice.SortOrder
                        });
                    }
                }

                addOption.Choices = optionChoices;

                returnOptions.Add(addOption);
            }

            return(returnOptions);
        }