I want to create product variations for my own ecommerce website.
I have a probem when i want to know which variant is existing and which one is new, to insert or update it.
To create variants i use the following php function:
<pre>
function possible_combos($groups, $prefix='') {
$result = array();
$group = array_shift($groups);
foreach($group as $selected) {
if($groups) {
$result = array_merge($result, possible_combos2($groups, $prefix .$selected.', '));
} else {
$result[] = [
'combination_string' => $prefix.$selected,
];
}
}
return $result;
}
</pre>
With the following array for build:
<pre>
Array
(
[Color] => Array
(
[0] => Green
[1] => Red
)
[Storage] => Array
(
[0] => 128 GB
[1] => 256 GB
)
)
</pre>
Color: Green, Red Storage: 128 GB, 256 GB
And the result for this is :
<pre>
Array
(
[0] => Array
(
[combination_string] => Green, 128 GB
)
[1] => Array
(
[combination_string] => Green, 256 GB
)
[2] => Array
(
[combination_string] => Red, 128 GB
)
[3] => Array
(
[combination_string] => Red, 256 GB
)
)
</pre>
All works good on add mode, the problem is on edit mode.
For each variant combination i create a new product using 'Parent_id';
So in edit page a have this:
<pre>
Array
(
[0] => Array
(
[product_id] => 1,
[combination_string] => Green, 128 GB
)
[1] => Array
(
[product_id] => 2,
[combination_string] => Green, 256 GB
)
[2] => Array
(
[product_id] => 3,
[combination_string] => Red, 128 GB
)
[3] => Array
(
[product_id] => 4,
[combination_string] => Red, 256 GB
)
)
</pre>
If i add new attribute for combination for example:
Size: Big
<pre>
Array
(
[Color] => Array
(
[0] => Green
[1] => Red
)
[Storage] => Array
(
[0] => 128 GB
[1] => 256 GB
)
[Size] => Array
(
[0] => Big
)
)
</pre>
For this case i want to return
<pre>
Array
(
[0] => Array
(
[product_id] => 1,
[combination_string] => Green, 128 GB, Big
)
[1] => Array
(
[product_id] => 2,
[combination_string] => Green, 256 GB, Big
)
[2] => Array
(
[product_id] => 3,
[combination_string] => Red, 128 GB, Big
)
[3] => Array
(
[product_id] => 4,
[combination_string] => Red, 256 GB, Big
)
)
</pre>
And ... if i add a new value for attribute Storage 512 GB fro example: The result should looks like:
<pre>
Array
(
[0] => Array
(
[product_id] => 1,
[combination_string] => Green, 128 GB, Big
)
[1] => Array
(
[product_id] => 2,
[combination_string] => Green, 256 GB, Big
)
[2] => Array
(
[combination_string] => Green, 512 GB, Big
)
[3] => Array
(
[product_id] => 3,
[combination_string] => Red, 128 GB, Big
)
[4] => Array
(
[product_id] => 4,
[combination_string] => Red, 256 GB, Big
)
[5] => Array
(
[combination_string] => Red, 512 GB, Big
)
)
</pre>
No product_id for existing combs.
I don't know if i'm clear with this, but i can give more info if needed.
EDIT for @PHPnoob