I have created an e-commerce site and am trying to expand it. Previously each product could have an attribute, color for example, so when a user selects a product they can choose the color they prefer.
I am now trying to add multiple attributes for each product, so for example this could be color and size.
However, each variation of the product is priced differently, so I am trying to figure out how to do the pricing on the back end.
Unfortunately I can't really show all my code as there's a lot of additional stuff going on that is not really appropriate to the question such as suppliers giving us cost prices in different currencies and exchange rates etc,.
The main issue is when I choose a product the attributes that apply are in an array. Like this:
[0]=>Color,[1]=>Size
Before when it was just one attribute this was fine I could query Color
from my database and do something like:
foreach($color_query as $color){
//generate form for pricing
}
Now, with multiple attributes, I want to do something like:
foreach(combination of colors and sizes){
//generate form for pricing
}
So what I'm looking for would be something like:
Small Red
Medium Red
Large Red
Small Blue
Medium Blue
Large Blue
Small Green
Medium Green
Large Green
Note: Size and Color are just examples, I will have multiple attributes and these can have multiple values, so using these examples I might have up to 5 sizes but maybe 15 colors.
How can I get the combination of colors and sizes?
Edit
I'm going to give snippets from the code to try give a better understanding.
I query my database for a product and this gives me back a string of comma separated values which are the ID's of my attribute sets
$attributes = $_GET['attribute'];
$attribute_array = explode(',',$attributes);
$attribute_array = array_diff($attribute_array, array("")); //this line just gets rid of the empty value after the last comma
foreach($attribute_array as $attribute){
//start generating table
$params = [$attribute];
$sql = "SELECT * FROM attributes WHERE id=?";
$attributeResult = DB::run($sql,$params);
foreach ($attributeResult as $value) {
//generate form for pricing to go into table
}
}
//close table
I do not think the suggestions in these other answers will work for me in this case as I am doing additional stuff in PHP in between the queries like getting the other stuff I need to generate the full pricing form like supplier currency and exchange etc,.