QuestionsWooCommerce: Insert products programmatically
mmclara clara asked 2 weeks ago

At first you need to insert the product with “product” post_type and get inserted post id:

$postid = wp_insert_post( array(
‘post_title’ => ‘testing product’,
‘post_content’ => ‘Here is content of the product description’,
‘post_status’ => ‘publish’,
‘post_type’ => “product”,
) );

Now set product term like simple, configurable:

wp_set_object_terms( $postid, ‘simple’, ‘product_type’ );

At the end you need to update the product post meta with your desirable value:

update_post_meta( $postid, ‘_visibility’, ‘visible’ );
update_post_meta( $postid, ‘_stock_status’, ‘instock’);
update_post_meta( $postid, ‘total_sales’, ‘0’ );
update_post_meta( $postid, ‘_downloadable’, ‘no’ );
update_post_meta( $postid, ‘_virtual’, ‘yes’ );
update_post_meta( $postid, ‘_regular_price’, ” );
update_post_meta( $postid, ‘_sale_price’, ” );
update_post_meta( $postid, ‘_purchase_note’, ” );
update_post_meta( $postid, ‘_featured’, ‘no’ );
update_post_meta( $postid, ‘_weight’, ” );
update_post_meta( $postid, ‘_length’, ” );
update_post_meta( $postid, ‘_width’, ” );
update_post_meta( $postid, ‘_height’, ” );
update_post_meta( $postid, ‘_sku’, ” );
update_post_meta( $postid, ‘_product_attributes’, array() );
update_post_meta( $postid, ‘_sale_price_dates_from’, ” );
update_post_meta( $postid, ‘_sale_price_dates_to’, ” );
update_post_meta( $postid, ‘_price’, ” );
update_post_meta( $postid, ‘_sold_individually’, ” );
update_post_meta( $postid, ‘_manage_stock’, ‘no’ );
update_post_meta( $postid, ‘_backorders’, ‘no’ );
update_post_meta( $postid, ‘_stock’, ” );

Your Answer
Login
Enter email and password to comment or answer
Not the answer you are looking for? Browse other questions tagged or ask your own question.