首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在woocommerce上创建可变产品的SQL查询

问在woocommerce上创建可变产品的SQL查询
EN

Stack Overflow用户
提问于 2018-01-13 22:28:50
回答 2查看 2K关注 0票数 0

我正在尝试使用mysql和php创建可变产品。我发现woocommerce将产品存储在产品表中,但类型为‘wp_posts’

变体也保存在同一张表中,但使用'post_parent‘= produt id

同样在wp_term_relationships上,我为变量产品设置了term_taxonomy_id =6。

在wp_postmeta上,我添加了一些变化数据。

创建了产品,但没有任何变化。有人能帮我吗?

EN

回答 2

Stack Overflow用户

发布于 2018-01-18 09:51:52

每个变体都作为其自己的产品存储在wp_posts表中,并引用post_parent字段中的原始产品。

希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2018-01-22 16:05:08

如果您已经在创建变量product,并且想要向该产品添加变体,下面给出的代码将对您有所帮助。

1-假设你有一个属性,它的大小有变化,比如-

代码语言:javascript
复制
$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-现在你要做的就是像这样给产品添加变种--

代码语言:javascript
复制
$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++;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48240802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档