Title: Inserting Vendor + Product from Code
Last modified: April 7, 2020

---

# Inserting Vendor + Product from Code

 *  Resolved [elronaldo](https://wordpress.org/support/users/elronaldo/)
 * (@elronaldo)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/inserting-vendor-product-from-code/)
 * The code below DOES succeed in creating a new vendor merchant and inserting a
   product in the vendor store…
 * except…
 * 1) The Vendor does not appear in the vendors list when the [wcv_vendorslist] 
   short tag is used
 * 2) When the product is purchased from the vendor store, the sales does not appear
   in the vendor’s sale display
 * Any other meta-data (and so forth) that I must be setting to make this work?
 *     ```
       function diag(){
   
           $userdata = array(
           'user_email' => 'bhsspartans@theget.io',
           'user_login' => 'BHSSpartans',
           'display_name' => 'BHSSpartans',
           'user_nicename' => 'BHSSpartans',
           'nickname' => 'BHSSpartans',
           'first_name' => 'Bubba',
           'last_name' => 'Baggins',
           'role' => 'vendor',);
           $author = wp_insert_user($userdata);
   
           echo 'User ID: ' . $author . '<br />';
   
           $post_id = wp_insert_post(array(
               'post_author' => $author,
               'post_title' => 'Noble Fir 8 Foot',
               'post_type' => 'product',
               'post_status' => 'publish',
               'post_content' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.',
               'post_excerpt' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.'
           ));
   
           echo 'POST ID: ' . $post_id;
   
           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', '77.00' );
           update_post_meta( $post_id, '_sale_price', '67.00' );
           update_post_meta( $post_id, '_purchase_note', '' );
           update_post_meta( $post_id, '_featured', 'no' );
           update_post_meta( $post_id, '_weight', '11' );
           update_post_meta( $post_id, '_stock_status', 'instock' );
           update_post_meta( $post_id, '_length', '11' );
           update_post_meta( $post_id, '_width', '11' );
           update_post_meta( $post_id, '_height', '11' );
           update_post_meta( $post_id, '_sku', 'BHSSpartansNoFir8' );
           update_post_meta( $post_id, '_sale_price_dates_from', '' );
           update_post_meta( $post_id, '_sale_price_dates_to', '' );
           update_post_meta( $post_id, '_price', '67.00' );
           update_post_meta( $post_id, '_sold_individually', 'yes' );
           update_post_meta( $post_id, '_manage_stock', 'no' );
           update_post_meta( $post_id, '_backorders', 'no' );
           update_post_meta( $post_id, '_stock', 50 );
           wc_update_product_stock($post_id, 50, 'set');
   
       }
       ```
   

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

 *  [Jamie Madden](https://wordpress.org/support/users/digitalchild/)
 * (@digitalchild)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/inserting-vendor-product-from-code/#post-12648879)
 * Hello [@elronaldo](https://wordpress.org/support/users/elronaldo/)
 * I would suggest creating the product using the WooCommerce API, then with the
   newly created product id, update the underlying post to change the author. That
   should ensure that the product is created correctly first.
 * That is the only thing I could think of as to why your code isn’t working.
 * 1. Does the vendor show up if you update the has_products=”no”? This should remove
   the product filter and show all users with the vendor role.
 * 2. Does the product show assigned to the vendor in the WordPress admin?
 * cheers,
 * Jamie.
 *  Thread Starter [elronaldo](https://wordpress.org/support/users/elronaldo/)
 * (@elronaldo)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/inserting-vendor-product-from-code/#post-12653088)
 * >> has_products=”no”
 * This is usermeta? By updating it I should set it to ‘yes’?
 *  [Jamie Madden](https://wordpress.org/support/users/digitalchild/)
 * (@digitalchild)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/inserting-vendor-product-from-code/#post-12671226)
 * Hi [@elronaldo](https://wordpress.org/support/users/elronaldo/)
 * Apologies, this reply was buried in our logs. has_products is the shortcode argument
   to say get all vendors, not just vendors that have listed products for sale.
 * cheers,
 * Jamie.
 *  [Jamie Madden](https://wordpress.org/support/users/digitalchild/)
 * (@digitalchild)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/inserting-vendor-product-from-code/#post-12675507)
 * Hi [@elronaldo](https://wordpress.org/support/users/elronaldo/)
 * Were you able to successfully create the product via the WooCommerce APIs? I 
   did a quick test and this should be the correct way to do this with code.
 * cheers,
 * Jamie.
 *  Thread Starter [elronaldo](https://wordpress.org/support/users/elronaldo/)
 * (@elronaldo)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/inserting-vendor-product-from-code/#post-12675537)
 * Jamie – I had already added the has_products element after a direct database 
   record comparison with a Vendor who had been added ‘by hand’ – I’ll be testing
   on Wed to see if the vendor gets a directory listing OK, and if the sale is attributed
   to the vendor properly. I’ll let you know the result after I test.
 * Thanks for all your kind help….
 * Robinson
 *  [Jamie Madden](https://wordpress.org/support/users/digitalchild/)
 * (@digitalchild)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/inserting-vendor-product-from-code/#post-12675539)
 * Hi [@elronaldo](https://wordpress.org/support/users/elronaldo/)
 * Ok, please do let me know how it goes. The shortcode allows you to show all vendors
   regardless if they have any products, or only list those that have products. 
   This allows you to have a directory of only vendors that have products. The products
   attributed to them are based on a query to check the products post_author field
   for each vendor_id. So programmatically or usual product creation shouldn’t matter.
 * cheers,
 * Jamie.

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

The topic ‘Inserting Vendor + Product from Code’ is closed to new replies.

 * ![](https://ps.w.org/wc-vendors/assets/icon.svg?rev=1645455)
 * [WC Vendors - WooCommerce Multivendor, WooCommerce Marketplace, Product Vendors](https://wordpress.org/plugins/wc-vendors/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wc-vendors/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wc-vendors/)
 * [Active Topics](https://wordpress.org/support/plugin/wc-vendors/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wc-vendors/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wc-vendors/reviews/)

## Tags

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

 * 6 replies
 * 2 participants
 * Last reply from: [Jamie Madden](https://wordpress.org/support/users/digitalchild/)
 * Last activity: [6 years, 1 month ago](https://wordpress.org/support/topic/inserting-vendor-product-from-code/#post-12675539)
 * Status: resolved