In WordPress, I have a function which allows me to automatically add a woocommerce product by adding a new post of a custom post type called ‘daily_cartoon’.
This the code of the function:
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');
The new product shows up nicely in the admin area, with all the data there – i.e. price, category, published, etc.
But for some reason it doesn’t show up on the website shop page until I open the new product to edit and click update.
Anybody?
Make sure you set the _visibility meta of the product to visible
For those comming here
Don’t forget to check your datas, I got a similar problem and it’s because WP sanitize datas once you update them manually from the dashboard. That’s why it worked for those updated manually.
Into my database, my datas got some hidden spaces (example : “Paris” / ” Paris”).
In case you need to import products, consider to use the wp function sanitize_text_field()