Exemple #1
0
        protected virtual bool ConvertVariantAlias(ProductVariantQuery query, string key, ICollection <string> values, int languageId)
        {
            if (values.Count == 0)
            {
                return(false);
            }

            var ids = key.SplitSafe("-");
            var len = ids.Length;

            if (len < 4)
            {
                return(false);
            }

            var alias = string.Join("-", ids.Take(len - 3));

            var attributeId = _catalogSearchQueryAliasMapper.GetVariantIdByAlias(alias, languageId);

            if (attributeId == 0)
            {
                return(false);
            }

            var productId          = ids.SafeGet(len - 3).ToInt();
            var bundleItemId       = ids.SafeGet(len - 2).ToInt();
            var variantAttributeId = ids.SafeGet(len - 1).ToInt();

            if (productId == 0 || variantAttributeId == 0)
            {
                return(false);
            }

            foreach (var value in values)
            {
                // We cannot use GetVariantOptionIdByAlias. It doesn't necessarily provide a ProductVariantAttributeValue.Id associated with this product.
                //var optionId = _catalogSearchQueryAliasMapper.GetVariantOptionIdByAlias(value, attributeId, languageId);
                var    optionId   = 0;
                string valueAlias = null;
                var    valueIds   = value.SplitSafe("-");
                if (valueIds.Length >= 2)
                {
                    optionId   = valueIds.SafeGet(valueIds.Length - 1).ToInt();
                    valueAlias = string.Join("-", valueIds.Take(valueIds.Length - 1));
                }

                var variant = new ProductVariantQueryItem(optionId == 0 ? value : optionId.ToString());
                variant.ProductId          = productId;
                variant.BundleItemId       = bundleItemId;
                variant.AttributeId        = attributeId;
                variant.VariantAttributeId = variantAttributeId;
                variant.Alias = alias;

                if (optionId != 0)
                {
                    variant.ValueAlias = valueAlias;
                }

                query.AddVariant(variant);
            }

            return(true);
        }
        protected virtual void ConvertVariantAlias(ProductVariantQuery query, string key, ICollection <string> values, int languageId)
        {
            var ids = key.SplitSafe("-");
            var len = ids.Length;

            if (len < 4)
            {
                return;
            }

            var isDate = key.EndsWith("-date") || key.EndsWith("-year");
            var isFile = key.EndsWith("-file");
            var isText = key.EndsWith("-text");

            if (isDate || isFile || isText)
            {
                ids = ids.Take(len - 1).ToArray();
                len = ids.Length;
            }

            var alias       = string.Join("-", ids.Take(len - 3));
            var attributeId = _catalogSearchQueryAliasMapper.GetVariantIdByAlias(alias, languageId);

            if (attributeId == 0)
            {
                return;
            }

            var productId          = ids.SafeGet(len - 3).ToInt();
            var bundleItemId       = ids.SafeGet(len - 2).ToInt();
            var variantAttributeId = ids.SafeGet(len - 1).ToInt();

            if (productId == 0 || variantAttributeId == 0)
            {
                return;
            }

            if (isDate || isFile || isText)
            {
                var value   = isText ? string.Join(",", values) : values.First();
                var variant = new ProductVariantQueryItem(value)
                {
                    ProductId          = productId,
                    BundleItemId       = bundleItemId,
                    AttributeId        = attributeId,
                    VariantAttributeId = variantAttributeId,
                    Alias  = alias,
                    IsFile = isFile,
                    IsText = isText
                };

                if (isDate)
                {
                    variant.Date = ConvertToDate(key, value);
                }

                query.AddVariant(variant);
            }
            else
            {
                foreach (var value in values)
                {
                    // We cannot use GetVariantOptionIdByAlias. It doesn't necessarily provide a ProductVariantAttributeValue.Id associated with this product.
                    //var optionId = _catalogSearchQueryAliasMapper.GetVariantOptionIdByAlias(value, attributeId, languageId);
                    var    optionId   = 0;
                    string valueAlias = null;

                    var valueIds = value.SplitSafe("-");
                    if (valueIds.Length >= 2)
                    {
                        optionId   = valueIds.SafeGet(valueIds.Length - 1).ToInt();
                        valueAlias = string.Join("-", valueIds.Take(valueIds.Length - 1));
                    }

                    var variant = new ProductVariantQueryItem(optionId == 0 ? value : optionId.ToString())
                    {
                        ProductId          = productId,
                        BundleItemId       = bundleItemId,
                        AttributeId        = attributeId,
                        VariantAttributeId = variantAttributeId,
                        Alias = alias
                    };

                    if (optionId != 0)
                    {
                        variant.ValueAlias = valueAlias;
                    }

                    query.AddVariant(variant);
                }
            }
        }