We are trying to make a small shop with product variants. So, for instance, a red, medium sized shirt. The SKU (or product code) will be a combination of all the information. So, the SKU for a shirt might be "ABC", we then want to append the ID's of the "red" and "medium" to the SKU to generate a complete collection of possible options so we can then assign stock to them (Hope I made this clear).
I'm a bit stuck as to how I can generate all these unique SKU's nicely without having duplicates (e.g. we only want "ABC-Red-Medium" without "ABC-Medium-Red"). I have this code at the moment, but am a bit lost - someone please help me!
Thank you!
Code: Select all
<?php
// All possible values
$start = array(
'colour' => array(1 => '-red', 2 => '-blue', 3 => '-pink'),
'size' => array(20 => '-small', 21 => '-medium', 22 => 'large'),
'arrow' => array(30 => '-up', 31 => '-right', 32 => '-down')
);
// All the combinations
$end = array();
// Loop over the first one
foreach ($start[0] as $id => $value) {
// Start to loop over the other variants
for ($i = 1; $i < count($start); $i++) {
// Loop over each of the variants
foreach ($start[$i] as $id => $value) {
$end[] = $skuNew . $value;
}
}
}
// Print
echo '<pre>';
var_dump($end);
echo '</pre>';