I am developing an inventory management system and I want to add a product with variants. I can add a product with three variants(color, size, material) and the options for each as below:
color - Black, Blue, Grey size - S,M,L,XL material - Cotton, Wool
If specify only 2 variants(e.g. color and size) my code is generating all the options correctly but if I add a 3rd variant then its not giving me the expected output.
Suppose I have a product called Jean my expected output would be as below:
- Jean-Black/S/Cotton
- Jean-Black/S/Wool
- Jean-Black/M/Cotton
- Jean-Black/M/Wool
- Jean-Black/L/Cotton
- Jean-Black/L/Wool
- Jean-Black/XL/Cotton
- Jean-Black/XL/Wool
===========================================
- Jean-Blue/S/Cotton
- Jean-Blue/S/Wool
- Jean-Blue/M/Cotton
- Jean-Blue/M/Wool
- Jean-Blue/L/Cotton
- Jean-Blue/L/Wool
- Jean-Blue/XL/Cotton
- Jean-Blue/XL/Wool
===========================================
- Jean-Grey/S/Cotton
- Jean-Grey/S/Wool
- Jean-Grey/M/Cotton
- Jean-Grey/M/Wool
- Jean-Grey/L/Cotton
- Jean-Grey/L/Wool
- Jean-Grey/XL/Cotton
- Jean-Grey/XL/Wool
My model is as below:
public class CreateModel : PageModel
{
[Required]
[BindProperty]
public string? Name { get; set; }
[BindProperty]
public List<ProductVariantModel> Variants { get; set; }
}
ProductVariantModel
public class ProductVariantModel
{
public string? Name { get; set; }
public string? Options { get; set; }
}
I'm creating the combinations as below:
List<ProductVariantOption> productOptions = new();
try
{
int variantsTotal = model.Variants.Count;
for (int a = 0; a < variantsTotal; a++)
{
string[] options = model.Variants[a].Options.Split(',');
for (int i = 0; i < options.Length; i++)
{
string? option = $"{model.Name}-{options[i]}";
if (variantsTotal > 1)
{
int index = a + 1;
if (index < variantsTotal)
{
var levelBelowOptions = model.Variants[index].Options.Split(',');
var ops = GetOptions(option, levelBelowOptions);
productOptions.AddRange(ops);
}
}
}
a += 1;
}
}
GetOptions method
private List<ProductVariantOption> GetOptions(string option, string[] options)
{
List<ProductVariantOption> variantOptions = new();
for (int i = 0; i < options.Length; i++)
{
string sku = $"{option}/{options[i]}";
string opt = $"{option}/{options[i]}";
variantOptions.Add(new ProductVariantOption(opt, sku));
}
return variantOptions;
}
ProductVariantOption
public class ProductVariantOption
{
public string Name { get; private set; }
public string SKU { get; private set; }
public Guid ProductVariantId { get; private set; }
public ProductVariant ProductVariant { get; private set; }
public ProductVariantOption(string name, string sku)
{
Guard.AgainstNullOrEmpty(name, nameof(name));
Name = name;
SKU = sku;
}
}
Where am I getting it wrong?