Use the below code to add product in woocommerce pro-grammatically.

<?php
    //adding woocommerce product
  $post = array(
    //Product description
    'post_content' => 'testcontent',
    'post_status' => "publish",
    //Product title
    'post_title' => 'Product Name',
    'post_parent' => '',
    'post_type' => "product",
);
$wp_error=false;
//Create post
$post_id = wp_insert_post( $post, $wp_error );
//setting product category
wp_set_object_terms( $post_id, 'testcategory', 'product_cat' );
//Product type
wp_set_object_terms($post_id, 'simple', 'product_type');

update_post_meta( $post_id, '_visibility', 'visible' );
update_post_meta( $post_id, '_stock_status', 'instock');
update_post_meta( $post_id, 'total_sales', '0');
update_post_meta( $post_id, '_downloadable', '');
update_post_meta( $post_id, '_virtual', '');
update_post_meta( $post_id, '_regular_price','100' );

update_post_meta( $post_id, '_sale_price', '');
update_post_meta( $post_id, '_purchase_note', "" );
update_post_meta( $post_id, '_featured', "no" );
update_post_meta( $post_id, '_weight', "" );
update_post_meta( $post_id, '_length', "" );
update_post_meta( $post_id, '_width', "" );
update_post_meta( $post_id, '_height', "" );
update_post_meta($post_id, '_sku', "");
update_post_meta( $post_id, '_product_attributes', array());
update_post_meta( $post_id, '_sale_price_dates_from', "" );
update_post_meta( $post_id, '_sale_price_dates_to', "" );
update_post_meta( $post_id, '_price','100' );
update_post_meta( $post_id, '_sold_individually', "" );
update_post_meta( $post_id, '_manage_stock', "no" );
update_post_meta( $post_id, '_backorders', "no" );
update_post_meta( $post_id, '_stock', "" );

?>

Categories: WordPress

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *