1

enter image description here 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. enter image description here

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

Dobra Adrian
  • 163
  • 1
  • 6

1 Answers1

0

You need a function that could do multiple arrays permutation.

I'll simply paste below a corresponding function from an another SO thread :

function cartesian($input) {
    $result = array(array());

    foreach ($input as $key => $values) {
        $append = array();

        foreach($result as $product) {
            foreach($values as $item) {
                $product[$key] = $item;
                $append[] = $product;
            }
        }

        $result = $append;
    }

    return $result;
}
PHPnoob
  • 586
  • 3
  • 7
  • Thx for your answer, but i have the cartesian function, my problem is how do i know which combination is new and which one is old. I need a way to merge DB combinations query array with New combination array. – Dobra Adrian Mar 19 '19 at 17:42
  • How are you going to filter from this table since the only differences are carried in the single column "name" ? Guess you have to loop over each row and insert the new one if it doens't exist yet... – PHPnoob Mar 19 '19 at 17:57
  • It's now looks even weirder to me. Case 2 : Why are there a parent id which is 0 and three which are set to 1 ? Case 3 : You can't expect two ids to be egal, since by definition they're unique... – PHPnoob Mar 20 '19 at 10:34
  • So either you re-structure properly your database, or you try to go with that anyway so you'll need to set the "product_name" column unique and use a MySQL INSERT IGNORE statement. – PHPnoob Mar 20 '19 at 10:37