我正在使用脚本将多个属性导入到变量产品中。
我得到以下格式的属性。
代码脚本如下
function products_options_values($a) {
$result = array(array());
foreach ($a as $k => $list) {
$_tmp = array();
foreach ($result as $result_item) {
foreach ($list as $list_item) {
$_tmp[] = array_merge($result_item, array($k => $list_item));
}
}
$result = $_tmp;
}
return $result;
} if($attributes = get_result("select * from products_attributes where products_id = X ")){
wp_set_object_terms($product_id, 'variable', 'product_type');
$attrib_array = array();
$attrib_combo = array();
$max_price = $product['products_price'];
$min_price = $product['products_price'];
foreach ($attributes as $attribute) {
$slug = sanitize_title($attribute['products_options_name']);
$attrib_array[$slug] = array(
'name' => $attribute['products_options_name'],
'value' => ltrim($attrib_array[$slug]['value'] . ' | ' . $attribute['products_options_values_name'], ' | '),
'position' => 0,
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 0);
if($attribute['price_prefix'] == '-')$attributeprefix = '-';
else if($attribute['price_prefix'] == '+')$attributeprefix = '+';
else $attributeprefix = '';
$attrib_combo[$slug][] = array($attribute['products_options_values_name'], $attributeprefix . $attribute['options_values_price']);
}
$combos = products_options_values($attrib_combo);
foreach ($combos as $combo) {
$variation_id = wp_insert_post(array(
'post_title' => 'Product ' . $product_id . ' Variation',
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'product_variation',
'post_author' => 1,
'post_parent' => $product_id
));
$opt_price = $product['products_price'] .$attributeprefix. $attribute['options_values_price'];
$special_price = $special['specials_new_products_price'];
foreach ($combo as $k => $v) {
update_post_meta($variation_id, 'attribute_' . $k, $v[0]);
$opt_price += $v[1];
$special_price += $v[1];
}
update_post_meta($variation_id, '_regular_price', $opt_price);
update_post_meta($variation_id, '_price', $opt_price);
update_post_meta($variation_id, '_thumbnail_id', 0);
update_post_meta($variation_id, '_stock', $product['products_quantity']);
if ($opt_price > $max_price) {
$max_price = $opt_price;
}
if ($opt_price < $min_price) {
$min_price = $opt_price;
}
}
update_post_meta($product_id, '_product_attributes', $attrib_array);
update_post_meta($product_id, '_max_variation_regular_price', $max_price);
update_post_meta($product_id, '_min_variation_regular_price', $min_price);
update_post_meta($product_id, '_max_variation_price', $max_price);
update_post_meta($product_id, '_min_variation_price', $min_price);}
使用此代码,我可以成功创建属性和变体
,但是当我在网站上看到并选择可变产品并单击添加到购物车按钮错误显示时
发布的值无效
我不明白哪里有错误或缺少一些要涵盖的元字段。
如果我缺少一些元字段或一些代码问题,请帮助我的前辈。