所以我有这个数组(只是举个例子,它是动态构建的):
$v_values = array(
array("C1","C2"),
array("F1","F2")
);
$v_name = array("color","fabric");
$v_price = array(
array(1000,2000),
array(3000,4000)
);
现在我想创建一个有变化的产品,所以这是代码:
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]);
}
}
}
}
}
假设$ins
变量已定义并且$post_id
是父帖子。
问题:
上面的代码创建了以下变体:
| C1 | F2 |
---------
| C2 | F2 |
---------
| C2 | F1 |
---------
| C2 | F2 |
预期:
| C1 | F1 |
---------
| C1 | F2 |
---------
| C2 | F1 |
---------
| C2 | F2 |
想不出办法,求大神帮忙!