在Wordpress中,我有一个函数,允许我通过添加一个名为"daily_cartoon"的自定义帖子类型的新帖子来自动添加woocommerce产品。
这是函数的代码:
function convert_daily_cartoon_into_product($post_id){
if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {
// Create post object
$post = array(
'post_title' => $_POST['post_title'],
'post_type' => 'product',
'post_status' => 'publish',
'tax_input' => array( 'product_cat' => 14 )
);
// Insert the post into the database
$new_print = wp_insert_post( $post );
update_post_meta( $new_print, '_thumbnail_id', get_post_thumbnail_id( $post_id ) );
update_post_meta( $new_print, '_regular_price', 5 );
update_post_meta( $new_print, '_price', 5 );
}
}
add_action('publish_daily_cartoon', 'convert_daily_cartoon_into_product');
新产品很好地显示在管理区域,所有数据都在那里 - 即价格、类别、发布等。
但由于某种原因,它不会显示在网站商店页面上,直到我打开新产品进行编辑并单击更新。
任何人?
确保将产品的_visibility元设置为可见
对于那些来这里的人
不要忘记检查您的数据,我遇到了类似的问题,这是因为一旦您从仪表板手动更新数据,WP 就会清理数据。这就是为什么它适用于那些手动更新的人。在我的数据库中,我的数据有一些隐藏的空间(例如:"巴黎"/"巴黎")。
如果您需要导入产品,请考虑使用 wp 函数 sanitize_text_field()