Title: Created Simple product programatically
Last modified: July 13, 2023

---

# Created Simple product programatically

 *  Resolved [Hannan](https://wordpress.org/support/users/phpcoderhannan/)
 * (@phpcoderhannan)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/created-simple-product-programatically/)
 * Hello,
 * I am trying to create simple product programmatically and when i going to apply
   below code then i got some warning. see this screenshot( [https://prnt.sc/N3WcJaZr_DO-](https://prnt.sc/N3WcJaZr_DO-)).
   even after those warning product added more than one also.
 * My code:
 * $product = new WC_Product_Simple();
 * $product->set_name(“Apple Watch”);
   $product->set_slug(“apple-watch”);
 * $product->set_short_description(“Excellent Product”);
 * $product->set_regular_price(2500);
   $product->set_sale_price(500);
 * $product->set_image_id(14);
   $product->set_category_ids(array(20));
 * $product->save();
 * By the way, I am using WordPress version: 6.2.2 and WooCommerce version is 7.8.2

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Ahir Hemant](https://wordpress.org/support/users/hemant-ahir/)
 * (@hemant-ahir)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/created-simple-product-programatically/#post-16893698)
 * Hi
   try this way
 *     ```wp-block-code
       $item = array(
           'Name' => 'Product A',
           'Description' => 'This is my test product A',
           'SKU' => '10085WE',
       );
       $user_id = get_current_user(); // this has NO SENSE AT ALL, because wp_insert_post uses current user as default value
       // $user_id = $some_user_id_we_need_to_use; // So, user is selected..
       $post_id = wp_insert_post( array(
           'post_author' => $user_id,
           'post_title' => $item['Name'],
           'post_content' => $item['Description'],
           'post_status' => 'publish',
           'post_type' => "product",
       ) );
       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', 'no' );
       update_post_meta( $post_id, '_virtual', 'yes' );
       update_post_meta( $post_id, '_regular_price', '' );
       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', $item['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', '' );
       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', '' );
       ```
   
 * Thanks
   Ahir Hemant
 *  Thread Starter [Hannan](https://wordpress.org/support/users/phpcoderhannan/)
 * (@phpcoderhannan)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/created-simple-product-programatically/#post-16894323)
 * Hello Ahir Hemant,
 * You are welcome. by the way, right now I do not want to try this way. Actually
   i need to know why those warning are coming. So if you know that, please shear
   with me.
 * Thank’s
 * Hannan
 *  [Beauty of Code (woo-hc)](https://wordpress.org/support/users/beautyofcode/)
 * (@beautyofcode)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/created-simple-product-programatically/#post-16902535)
 * Hi [@phpcoderhannan](https://wordpress.org/support/users/phpcoderhannan/) ,
 * Thanks for reaching out!
 * I understand that you need help with the errors you are seeing when applying 
   your custom code which allows you to programmatically create a simple product,
   is this correct?
 * Kindly note that custom code is outside [our scope of support](https://woocommerce.com/support-policy/).
   For help with custom code, you could [hire a developer](https://woocommerce.com/customizations/)
   or ask your customization questions in one of the channels below.
    - [WooCommerce Advanced Facebook Group](https://href.li/?https://www.facebook.com/groups/advanced.woocommerce/)
    - [WooCommerce Developer Slack Channel](https://href.li/?https://woocommerce.com/community-slack/)
 * Hope this points you in the right direction!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Created Simple product programatically’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [simple product](https://wordpress.org/support/topic-tag/simple-product/)

 * 3 replies
 * 3 participants
 * Last reply from: [Beauty of Code (woo-hc)](https://wordpress.org/support/users/beautyofcode/)
 * Last activity: [2 years, 10 months ago](https://wordpress.org/support/topic/created-simple-product-programatically/#post-16902535)
 * Status: resolved