Exemplo n.º 1
0
        /// <summary>
        /// Deserializes attributes XML into a product variant query
        /// </summary>
        /// <param name="query">Product variant query</param>
        /// <param name="productId">Product identifier</param>
        /// <param name="bundleItemId">Bundle item identifier</param>
        /// <param name="attributesXml">XML formatted attributes</param>
        public virtual void DeserializeQuery(ProductVariantQuery query, int productId, string attributesXml, int bundleItemId = 0)
        {
            Guard.NotNull(query, nameof(query));
            Guard.NotZero(productId, nameof(productId));

            if (attributesXml.IsEmpty() || productId == 0)
            {
                return;
            }

            var attributeMap = _productAttributeParser.DeserializeProductVariantAttributes(attributesXml);
            var attributes   = _productAttributeService.GetProductVariantAttributesByIds(attributeMap.Keys);

            foreach (var attribute in attributes)
            {
                foreach (var originalValue in attributeMap[attribute.Id])
                {
                    var      value = originalValue;
                    DateTime?date  = null;

                    if (attribute.AttributeControlType == AttributeControlType.Datepicker)
                    {
                        date = originalValue.ToDateTime(new string[] { "D" }, CultureInfo.CurrentCulture, DateTimeStyles.None, null);
                        if (date == null)
                        {
                            continue;
                        }

                        value = string.Join("-", date.Value.Year, date.Value.Month, date.Value.Day);
                    }

                    var queryItem = new ProductVariantQueryItem(value);
                    queryItem.ProductId          = productId;
                    queryItem.BundleItemId       = bundleItemId;
                    queryItem.AttributeId        = attribute.ProductAttributeId;
                    queryItem.VariantAttributeId = attribute.Id;
                    queryItem.Alias  = _catalogSearchQueryAliasMapper.Value.GetVariantAliasById(attribute.ProductAttributeId, _languageId);
                    queryItem.Date   = date;
                    queryItem.IsFile = attribute.AttributeControlType == AttributeControlType.FileUpload;
                    queryItem.IsText = attribute.AttributeControlType == AttributeControlType.TextBox || attribute.AttributeControlType == AttributeControlType.MultilineTextbox;

                    if (attribute.ShouldHaveValues())
                    {
                        queryItem.ValueAlias = _catalogSearchQueryAliasMapper.Value.GetVariantOptionAliasById(value.ToInt(), _languageId);
                    }

                    query.AddVariant(queryItem);
                }
            }
        }