/// <summary> /// Get product variant attribute values /// </summary> /// <param name="attributes">Attributes</param> /// <returns>Product variant attribute values</returns> public static ProductVariantAttributeValueCollection ParseProductVariantAttributeValues(string attributes) { var pvaValues = new ProductVariantAttributeValueCollection(); var pvaCollection = ParseProductVariantAttributes(attributes); foreach (var pva in pvaCollection) { if (!pva.ShouldHaveValues) { continue; } var pvaValuesStr = ParseValues(attributes, pva.ProductVariantAttributeId); foreach (string pvaValueStr in pvaValuesStr) { if (!String.IsNullOrEmpty(pvaValueStr)) { int pvaValueId = 0; if (int.TryParse(pvaValueStr, out pvaValueId)) { var pvaValue = ProductAttributeManager.GetProductVariantAttributeValueById(pvaValueId); if (pvaValue != null) { pvaValues.Add(pvaValue); } } } } } return(pvaValues); }
/// <summary> /// Formats attributes /// </summary> /// <param name="productVariant">Product variant</param> /// <param name="attributes">Attributes</param> /// <param name="customer">Customer</param> /// <param name="serapator">Serapator</param> /// <param name="htmlEncode">A value indicating whether to encode (HTML) values</param> /// <param name="renderPrices">A value indicating whether to render prices</param> /// <param name="renderProductAttributes">A value indicating whether to render product attributes</param> /// <param name="renderGiftCardAttributes">A value indicating whether to render gift card attributes</param> /// <returns>Attributes</returns> public static string FormatAttributes(ProductVariant productVariant, string attributes, Customer customer, string serapator, bool htmlEncode, bool renderPrices, bool renderProductAttributes, bool renderGiftCardAttributes) { var result = new StringBuilder(); //attributes if (renderProductAttributes) { var pvaCollection = ParseProductVariantAttributes(attributes); for (int i = 0; i < pvaCollection.Count; i++) { var pva = pvaCollection[i]; var valuesStr = ParseValues(attributes, pva.ProductVariantAttributeId); for (int j = 0; j < valuesStr.Count; j++) { string valueStr = valuesStr[j]; string pvaAttribute = string.Empty; if (!pva.ShouldHaveValues) { if (pva.AttributeControlType == AttributeControlTypeEnum.MultilineTextbox) { pvaAttribute = string.Format("{0}: {1}", pva.ProductAttribute.Name, HtmlHelper.FormatText(valueStr, false, true, true, false, false, false)); } else { pvaAttribute = string.Format("{0}: {1}", pva.ProductAttribute.Name, valueStr); } } else { var pvaValue = ProductAttributeManager.GetProductVariantAttributeValueById(Convert.ToInt32(valueStr)); if (pvaValue != null) { pvaAttribute = string.Format("{0}: {1}", pva.ProductAttribute.Name, pvaValue.Name); if (renderPrices) { decimal priceAdjustmentBase = TaxManager.GetPrice(productVariant, pvaValue.PriceAdjustment, customer); decimal priceAdjustment = CurrencyManager.ConvertCurrency(priceAdjustmentBase, CurrencyManager.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency); if (priceAdjustmentBase > 0) { string priceAdjustmentStr = PriceHelper.FormatPrice(priceAdjustment, false, false); pvaAttribute += string.Format(" [+{0}]", priceAdjustmentStr); } } } } if (!String.IsNullOrEmpty(pvaAttribute)) { if (i != 0 || j != 0) { result.Append(serapator); } //we don't encode multiline textbox input if (htmlEncode && pva.AttributeControlType != AttributeControlTypeEnum.MultilineTextbox) { result.Append(HttpUtility.HtmlEncode(pvaAttribute)); } else { result.Append(pvaAttribute); } } } } } //gift cards if (renderGiftCardAttributes) { if (productVariant.IsGiftCard) { string giftCardRecipientName = string.Empty; string giftCardRecipientEmail = string.Empty; string giftCardSenderName = string.Empty; string giftCardSenderEmail = string.Empty; string giftCardMessage = string.Empty; GetGiftCardAttribute(attributes, out giftCardRecipientName, out giftCardRecipientEmail, out giftCardSenderName, out giftCardSenderEmail, out giftCardMessage); if (!String.IsNullOrEmpty(result.ToString())) { result.Append(serapator); } if (htmlEncode) { result.Append(HttpUtility.HtmlEncode(string.Format(LocalizationManager.GetLocaleResourceString("GiftCardAttribute.For"), giftCardRecipientName))); result.Append(serapator); result.Append(HttpUtility.HtmlEncode(string.Format(LocalizationManager.GetLocaleResourceString("GiftCardAttribute.From"), giftCardSenderName))); } else { result.Append(string.Format(LocalizationManager.GetLocaleResourceString("GiftCardAttribute.For"), giftCardRecipientName)); result.Append(serapator); result.Append(string.Format(LocalizationManager.GetLocaleResourceString("GiftCardAttribute.From"), giftCardSenderName)); } } } return(result.ToString()); }