以编程方式创建的 Woocommerce 产品变体未显示在前端
首先插入父变量产品:
$parent_product = array(
'post_title' => 'Euro Washing Machine',
'post_content' => 'Some Description',
'post_type' => 'product',
'post_status' => 'publish',
'post_parent' => '',
'meta_input' => array(
'_rrp' => 220,
'_sku' =>'wedf4532d',
'_visibility' => 'visible'
)
);
$parent_product_id = wp_insert_post( $parent_product, true );
if ( ! $parent_product_id ) {
return 0;
}
wp_set_object_terms( $parent_product_id, 'variable', 'product_type' );
wp_set_object_terms( $parent_product_id, 'brand-new', 'pa_' . 'condition' );
$parent_data_attributes[ 'pa_' . 'condition' ] = array(
'name' => 'pa_' . 'condition',
'value' => 'brand-new',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
);
update_post_meta( $parent_product_id, '_product_attributes', $parent_data_attributes );
插入带有属性 pa_condition 的产品变体
$variation_product = array(
'post_title' => 'Euro Washing Machine',
'post_name' => 'product-' . $parent_product_id . '-variation',
'post_status' => 'publish',
'post_parent' => $parent_product_id,
'post_type' => 'product_variation'
);
$variation_product_id = wp_insert_post( $variation_product );
update_post_meta( $variation_product_id, '_price', 221 );
update_post_meta( $variation_product_id, '_sale_price', 221 );
update_post_meta( $variation_product_id, '_regular_price', 221 );
update_post_meta( $variation_product_id, '_manage_stock', 'true' );
update_post_meta( $variation_product_id, '_stock', 1 );
$variation = new WC_Product_Variation($variation_product_id);
$variation->set_attributes( array(
'pa_condition' => 'brand-new'
) );
$variation->save();
以编程方式添加的变体显示在管理区域但不显示在前端。如果我登录到管理员并点击更新产品,那么它就会出现在前面。
你能告诉我,我在这里缺少什么吗?
Inserting parent variable product first:
$parent_product = array(
'post_title' => 'Euro Washing Machine',
'post_content' => 'Some Description',
'post_type' => 'product',
'post_status' => 'publish',
'post_parent' => '',
'meta_input' => array(
'_rrp' => 220,
'_sku' =>'wedf4532d',
'_visibility' => 'visible'
)
);
$parent_product_id = wp_insert_post( $parent_product, true );
if ( ! $parent_product_id ) {
return 0;
}
wp_set_object_terms( $parent_product_id, 'variable', 'product_type' );
wp_set_object_terms( $parent_product_id, 'brand-new', 'pa_' . 'condition' );
$parent_data_attributes[ 'pa_' . 'condition' ] = array(
'name' => 'pa_' . 'condition',
'value' => 'brand-new',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
);
update_post_meta( $parent_product_id, '_product_attributes', $parent_data_attributes );
Insert Product Variation with attribute pa_condition
$variation_product = array(
'post_title' => 'Euro Washing Machine',
'post_name' => 'product-' . $parent_product_id . '-variation',
'post_status' => 'publish',
'post_parent' => $parent_product_id,
'post_type' => 'product_variation'
);
$variation_product_id = wp_insert_post( $variation_product );
update_post_meta( $variation_product_id, '_price', 221 );
update_post_meta( $variation_product_id, '_sale_price', 221 );
update_post_meta( $variation_product_id, '_regular_price', 221 );
update_post_meta( $variation_product_id, '_manage_stock', 'true' );
update_post_meta( $variation_product_id, '_stock', 1 );
$variation = new WC_Product_Variation($variation_product_id);
$variation->set_attributes( array(
'pa_condition' => 'brand-new'
) );
$variation->save();
Programmatically added variation showing on admin area but not showing on Front-End. If I logged in to admin and just hit update the product then it appears on the front.
Can you please tell me, What I am missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于在创建所有变体后,在可变产品上设置了产品类别,因此我遇到了这个问题。
如果在创建所有变化之前,请在变量产品上设置类别,则可以解决该问题。现在,我可以看到我的类别页面上的所有变体。
I got this problem due to setting up the product category on the variable product after creating all variations.
If I set the category on the variable product before creating all the variations then it solved the issue. Now I can see all variations on my category pages.