Woocommerce linking variations

So I have this array (just taking an example, it’s dynamically built up):

$v_values = array(
              array("C1","C2"),
              array("F1","F2")
            );

$v_name = array("color","fabric");

$v_price = array(
             array(1000,2000),
             array(3000,4000)
           );

Now I want to create a product with variation, so this is the code:

    for($i = 0; $i < count($v_name); $i++) {
        for($j = 0; $j < count($v_values[$i]); $j++) {

            $var_post = array(
                            'post_title'   => $ins["title"],
                            'post_content' => $ins["desc"] ,
                            'post_status'  => ($user->roles[0] === "pending_vendor") ? 'pending' : 'publish',
                            'post_parent' => $post_id,
                            'post_type' => 'product_variation',
                        );

            // Insert the post into the database
            $var_post_id = wp_insert_post( $var_post );

            update_post_meta($var_post_id, 'attribute_' . $v_name[$i], $v_values[$i][$j]);
            update_post_meta($var_post_id, '_price', $v_price[$i][$j]);
            update_post_meta($var_post_id, '_regular_price', (string) $v_price[$i][$j]);

            for($k = 0; $k < count($v_name); $k++) {
                if($i !== $k) {
                    for($l = 0; $l < count($v_values[$k]); $l++) {
                        update_post_meta($var_post_id, 'attribute_' . $v_name[$k], $v_values[$k][$l]);
                        update_post_meta($var_post_id, '_price', $v_price[$k][$l]);
                        update_post_meta($var_post_id, '_regular_price', (string) $v_price[$k][$l]);
                    }
                }
            }

        }
    }

Assume $ins variable is defined and $post_id is the parent post.

The problem:

The above code creates the following variation:

| C1 | F2 |
 ---------
| C2 | F2 |
 ---------
| C2 | F1 |
 ---------
| C2 | F2 |

What was expected:

| C1 | F1 |
 ---------
| C1 | F2 |
 ---------
| C2 | F1 |
 ---------
| C2 | F2 |

Cannot figure out a way, please help!

Answer

I found a solution with a recursive function

function decomposePrice($base, $iValue, $values, $prices) {

    $d = array();

    foreach ($prices as $i => $p) {

        $baseP = "$base{$values[$iValue][$i]}|";

        if (!is_array($p)) {
            $d[$baseP] = $p;
        } else {
            $d = array_merge($d, decomposePrice($baseP, $iValue + 1, $values, $p));
        }

    }

    return $d;
}


$decomposePrice = decomposePrice("", 0, $v_values, $v_price);


foreach ($decomposePrice as $att => $price) {


    $var_post = array(
                    'post_title'   => $ins["title"],
                    'post_content' => $ins["desc"] ,
                    'post_status'  => ($user->roles[0] === "pending_vendor") ? 'pending' : 'publish',
                    'post_parent' => $post_id,
                    'post_type' => 'product_variation',
                );

    // Insert the post into the database
    $var_post_id = wp_insert_post( $var_post );

    update_post_meta($var_post_id, '_price', $price);
    update_post_meta($var_post_id, '_regular_price', (string) $price);


    // attributes

    $list = explode("|", $att);
    array_pop($list);

    foreach ($list as $iName => $value) {
        update_post_meta($var_post_id, 'attribute_' . $v_name[$iName], $value);
    }

}

Attribution
Source : Link , Question Author : Niket Malik , Answer Author : mmm

Leave a Comment