I'm working on a shopping site and want to give my users the ability to dynamically create multiple products based on variants they specify. I allow these users to create any number of 'Types', which define things like Size, Colour, or Capacity. But the user can specify the name of this and the number of types they want.
Within each Type, I also allow the user to configure multiple Choices. For example, the type Size might have choices Small, Medium, Large. Again the user can define the names of these fields and there shouldn't be any limit to how many fields they can create.
After the user has finished defining their product, I have an array for types and inside each type, an array for choices.
As a sample, a mobile phone product might have the following type and choice structure:
[
{
'id': 1,
'type': 'Colour',
'options': ['Black', 'White', 'Gold']
},
{
'id': 2,
'type': 'Size',
'options': ['Standard', 'Large"']
},
{
'id': 3,
'type': 'Capacity',
'options': ['32GB', '64GB', '128GB']
}
]
From this data, I want to create 18 (3*2*3) possible product variants for which the user can specify specific price, weight, or SKU options. To end up with the following variants:
Black - Standard - 32GB
Black - Standard - 64GB
Black - Standard - 128GB
Black - Large - 32GB
Black - Large - 64GB
Black - Large - 128GB
White - Standard - 32GB
White - Standard - 64GB
White - Standard - 128GB
White - Large - 32GB
White - Large - 64GB
White - Large - 128GB
Gold - Standard - 32GB
Gold - Standard - 64GB
Gold - Standard - 128GB
Gold - Large - 32GB
Gold - Large - 64GB
Gold - Large - 128GB
Which I can imagine being in an array like this:
[
{
'variant_id': 1,
'options':
[{ 'type': 'Colour', 'choice': 'Black' },
{ 'type': 'Size', 'choice' 'Standard' },
{ 'type': 'Capacity', 'choice' '32G' }]
},
{
'variant_id': 2,
'options':
[{ 'type': 'Colour', 'choice': 'Black' },
{ 'type': 'Size', 'choice' 'Standard' },
{ 'type': 'Capacity', 'choice' '64GB' }]
},
{
'variant_id': 3,
'options':
[{ 'type': 'Colour', 'choice': 'Black' },
{ 'type': 'Size', 'choice' 'Standard' },
{ 'type': 'Capacity', 'choice' '128GB' }]
}
<snip>
My question is how I can create a loop that will allow me to iterate over the various types and choices to create my final variant list? I'm having a hard time visualising just how I could go about delivering this function.