【发布时间】:2018-12-02 10:09:04
【问题描述】:
我正在使用 wp_insert_post() 在 woocommerce 中以编程方式添加产品
除非我从管理员处单击更新,否则除了产品属性不会显示在产品页面视图上之外,我一切正常。
我正在添加大量产品,所以有什么方法可以在产品页面的下拉列表中显示变体。
这是我的代码,它工作正常,所有内容都放置在正确的字段中,它不会在不点击更新的情况下在产品页面上显示选项。
$new_post = array(
'ID' => '',
'post_author' => 1,
'post_title' => 'title of product',
'post_status' => 'publish',
'post_type' => 'product'
);
$post_id = wp_insert_post($new_post);
wp_set_object_terms($post_id, 'variable', 'product_type', false);
$my_post = array(
'post_title' => 'Variation # of ' . esc_attr(strip_tags( $post_title)),
'post_name' => 'product-' . $post_id . '-variation-',
'post_status' => 'publish',
'post_parent' => $post_id,
'post_type' => 'product_variation',
'guid' => home_url() . '/?product_variation=product-' . $post_id . '-variation-'
);
wp_insert_post( $my_post );
$variable_id = $post_id + 1;
update_post_meta( $variable_id, '_price', 8.50 );
update_post_meta( $variable_id, '_regular_price', '8.50');
$product_attributes['type'] = array(
'name' => htmlspecialchars(stripslashes('Options')),
'value' => "black|blue",
'position' => 1,
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 0
);
update_post_meta( $post_id, '_product_attributes', $product_attributes);
【问题讨论】:
-
不要像以前那样使用旧的方式,你应该使用new CRUD methods引入Woocommerce 3,就像this related thread和this other one一样。所以当你最后使用
save()方法时,所有的东西都会在各处同步更新(甚至是缓存的数据)。
标签: wordpress woocommerce