/// <summary> /// Creates a <see cref="IProductVariant" /> of the <see cref="IProduct" /> passed defined by the collection of /// <see cref="IProductAttribute" /> /// without saving it to the database /// </summary> /// <param name="product">The <see cref="IProduct" /></param> /// <param name="variants"> /// Existing variants to check against /// </param> /// <param name="attributes">The <see cref="IProductVariant" /></param> /// <returns> /// Either a new <see cref="IProductVariant" /> or, if one already exists with associated attributes, the existing /// <see cref="IProductVariant" /> /// </returns> internal IProductVariant CreateProductVariant(IProduct product, List <IProductVariant> variants, ProductAttributeCollection attributes) { var skuSeparator = MerchelloConfiguration.Current.DefaultSkuSeparator; // verify the order of the attributes so that a sku can be constructed in the same order as the UI var optionIds = product.ProductOptionsForAttributes(attributes).OrderBy(x => x.SortOrder).Select(x => x.Key) .Distinct(); // the base sku var sku = product.Sku; var name = string.Format("{0} - ", product.Name); foreach (var att in optionIds.Select(key => attributes.FirstOrDefault(x => x.OptionKey == key)) .Where(att => att != null)) { name += att.Name + " "; sku += skuSeparator + (string.IsNullOrEmpty(att.Sku) ? Regex.Replace(att.Name, "[^0-9a-zA-Z]+", string.Empty) : att.Sku); } return(CreateProductVariant(product, variants, name, sku, product.Price, attributes)); }
public static string GenerateSkuForVariant( IProduct product, IProductVariant variant, ProductAttributeCollection attributes) { var skuSeparator = MerchelloConfiguration.Current.DefaultSkuSeparator; // verify the order of the attributes so that a sku can be constructed in the same order as the UI var optionIds = product.ProductOptionsForAttributes(attributes).OrderBy(x => x.SortOrder).Select(x => x.Key).Distinct(); // the base sku var sku = product.Sku; // ReSharper disable once UseStringInterpolation var name = string.Format("{0} - ", product.Name); foreach (var att in optionIds.Select(key => attributes.FirstOrDefault(x => x.OptionKey == key)).Where(att => att != null)) { name += att.Name + " "; sku += skuSeparator + (string.IsNullOrEmpty(att.Sku) ? Regex.Replace(att.Name, "[^0-9a-zA-Z]+", string.Empty) : att.Sku); } return(sku); }
/// <summary> /// Creates a <see cref="IProductVariant"/> of the <see cref="IProduct"/> passed defined by the collection of <see cref="IProductAttribute"/> /// without saving it to the database /// </summary> /// <param name="product">The <see cref="IProduct"/></param> /// <param name="attributes">The <see cref="IProductVariant"/></param> /// <returns>Either a new <see cref="IProductVariant"/> or, if one already exists with associated attributes, the existing <see cref="IProductVariant"/></returns> internal IProductVariant CreateProductVariant(IProduct product, ProductAttributeCollection attributes) { var skuSeparator = MerchelloConfiguration.Current.DefaultSkuSeparator; // verify the order of the attributes so that a sku can be constructed in the same order as the UI var optionIds = product.ProductOptionsForAttributes(attributes).OrderBy(x => x.SortOrder).Select(x => x.Key).Distinct(); // the base sku var sku = product.Sku; var name = string.Format("{0} - ", product.Name); foreach (var att in optionIds.Select(key => attributes.FirstOrDefault(x => x.OptionKey == key)).Where(att => att != null)) { name += att.Name + " "; sku += skuSeparator + (string.IsNullOrEmpty(att.Sku) ? Regex.Replace(att.Name, "[^0-9a-zA-Z]+", string.Empty) : att.Sku); } return CreateProductVariant(product, name, sku, product.Price, attributes); }