我有一个数组(仅举一个例子,它是动态构建的):
$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
是父帖子
The problem:
上述代码创建了以下变体:
| C1 | F2 |
---------
| C2 | F2 |
---------
| C2 | F1 |
---------
| C2 | F2 |
预期:
| C1 | F1 |
---------
| C1 | F2 |
---------
| C2 | F1 |
---------
| C2 | F2 |
想不出办法,请帮忙!
SO网友:mmm
我找到了一个递归函数的解决方案
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);
}
}
SO网友:mmm
使用您的代码,您可以:
+++++++++++++++++++++++++++++++++++++++++++++++++++attribute\\u color=C1
attribute\\u fabric=F1
attribute\\u fabric=F2
attribute\\u fabric=F1
attribute\\u fabric=F2
attribute\\u fabric+++++++attribute\\u fabric=F1
attribute\\u color=C1
attribute\\u color=C2
+++++++++++++++++++++++++++++++++++++++++++++++++++attribute\\u fabric=F2
attribute\\u color=C1
attribute\\u color=C2
如果在$v\\u name中有2个名称,请尝试此操作以获得所有组合
foreach ($v_values[0] as $i0 => $v0) {
foreach ($v_values[1] as $i1 => $v1) {
$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[0], $v0);
update_post_meta($var_post_id, \'attribute_\' . $v_name[1], $v1);
update_post_meta($var_post_id, \'_price\', $v_price[$i0][$i1]);
update_post_meta($var_post_id, \'_regular_price\', (string) $v_price[$i0][$i1]);
}
}