我正在尝试使用 mysql 和 php 创建可变产品。 我发现 woocommerce 将产品存储在 wp_posts 表上,但类型为“产品”
此外,变体也保存在同一张表上,但带有“post_parent”=产品 ID
另外,在 wp_term_relationships 上,我为可变产品设置了 term_taxonomy_id = 6。
在 wp_postmeta 上我添加了一些变化数据。
产品已创建,但没有变化。 有人可以帮助我吗?
每个变体都作为其自己的产品存储在 wp_posts 表中,并引用 post_parent 字段中的原始产品。
希望这有帮助。
如果您已经在创建可变产品并想要向该产品添加变体,下面给出的代码将为您提供帮助。
1- 假设您有一个属性,即大小,其变化如 -
$avail_attributes = array(
'2xl',
'xl',
'lg',
);
wp_set_object_terms($post_id, $avail_attributes, 'pa_size');
$thedata = Array('pa_size'=>Array(
'name'=>'pa_size',
'value'=>'',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
));
update_post_meta( $new_post_id,'_product_attributes',$thedata);
update_post_meta( $post_id, '_stock_status', 'instock');
update_post_meta( $post_id, '_weight', "0.06" );
update_post_meta( $post_id, '_sku', "skutest1");
update_post_meta( $post_id, '_stock', "100" );
update_post_meta( $post_id, '_visibility', 'visible' );
2- 现在您要做的就是像这样向产品添加变体 -
$i=1;
while ($i<=5) {
$post = array(
'post_title'=> 'Variation #' . $i . ' of 5 for prdct#'. $post_id,
'post_name' => 'product-' . $post_id . '-variation-' . $i,
'post_status' => 'publish',
'post_parent' => $post_id,
'post_type' => 'product_variation',
'guid'=>home_url() . '/?product_variation=product-' . $post_id . '-variation-' . $i
);
$attID = wp_insert_post( $post );
$logtxt .= "Attribute inserted with ID: $attID\n";
$variation_id = $post_id + 1;
$variation_two = $variation_id + 1;
$variation_three = $variation_two + 1;
$variation_four = $variation_three + 1;
$variation_five = $variation_four + 1;
update_post_meta($variation_id, 'attribute_pa_size', '2xl'); update_post_meta($variation_id, '_price', 21.99);
update_post_meta($variation_id, '_regular_price', '21.99');
wp_set_object_terms($variation_id, $avail_attributes, 'pa_size');
$thedata = Array('pa_size'=>Array(
'name'=>'2xl',
'value'=>'',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
));
update_post_meta( $variation_id,'_product_attributes',$thedata);
update_post_meta( $variation_two, 'attribute_pa_size', 'xl');
update_post_meta( $variation_two, '_price', 20.99 );
update_post_meta( $variation_two, '_regular_price', '20.99');
wp_set_object_terms($variation_two, $avail_attributes, 'pa_size');
$thedata = Array('pa_size'=>Array(
'name'=>'xl',
'value'=>'',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
));
update_post_meta( $variation_two,'_product_attributes',$thedata);
update_post_meta( $variation_three, 'attribute_pa_size', 'lg');
update_post_meta( $variation_three, '_price', 18.99 );
update_post_meta( $variation_three, '_regular_price', '18.99');
wp_set_object_terms($variation_three, $avail_attributes, 'pa_size');
$thedata = Array('pa_size'=>Array(
'name'=>'lg',
'value'=>'',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
));
update_post_meta( $variation_three,'_product_attributes',$thedata);
update_post_meta( $variation_four, 'attribute_pa_size', 'md');
update_post_meta( $variation_four, '_price', 18.99 );
update_post_meta( $variation_four, '_regular_price', '18.99');
$i++;
}
答案很晚,但可能对其他人有帮助,因为我在同一个问题上花了几个小时:
基本上设置 post_parent 和 post_type 变化是不够的。
您还必须使用以下数据更新 wp_term_relationships 表:
object_id: {your post id}, term_taxonomy_id: {term_id from wp_terms where name = 'variable'}