cameronstark
7/13/2017 - 12:34 PM

Create Woocommerce Product | Taken from GWZ Cooking School Page | REF http://wordpress.stackexchange.com/questions/137501/how-to-add-product

$args = array(
  title => 'title here', 
  excerpt => 'excerpt here', 
  term => 'term here', 
  product_type => 'simple', 
  sku => '0000', 
  stock => '10000', 
  regular_price => 100,
  sale_price => 105,
  image_id => 1
);

function create_woocommerce_page( $args ) {
  //Turn args into variables
  extract( $args );
  //Create post
  $post = array(
    'post_author' => 1,
    'post_content' => '',
    'post_excerpt' => $excerpt,
    'post_status' => "publish",
    'post_title' => $title,
    'post_parent' => '',
    'post_type' => "product",
  );  
  $post_id = wp_insert_post( $post, $wp_error );
  //Add Category
  wp_set_object_terms( $post_id, $term, 'product_cat' );
  //Product Type
  wp_set_object_terms( $post_id, $product_type, 'product_type' );
  //Set Thumbnail
  set_post_thumbnail( $post_id, $image_id );
  //Meta
  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', $regular_price );
  //update_post_meta( $post_id, '_sale_price', $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', $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', $price );
  update_post_meta( $post_id, '_sold_individually', "" );
  update_post_meta( $post_id, '_manage_stock', "yes" );
  update_post_meta( $post_id, '_backorders', "yes" );
  update_post_meta( $post_id, '_stock', $stock );
  //Add Additional Meta here
}