Add product programmatically in woocommerce
Woocommerce is an open source WordPress plugin to build own online store. Woocommerce is the more popular eCommerce solution.It comes with many short-codes that help to expand functionality. In admin we have an option to add products.Programmatically we can insert product using following code.
$aProduct = new WC_Product(); $aProduct->set_name("Product Title"); $aProduct->set_status("publish"); $aProduct->set_catalog_visibility('visible'); $aProduct->set_description("Product Description"); $aProduct->set_sku("Product Sku"); $aProduct->set_price("Product price"); $aProduct->set_regular_price("Product regular price"); $aProduct->set_manage_stock(true); $aProduct->set_stock_quantity("Product stock"); $aProduct->set_stock_status('instock'); //image upload $mediaID = wpsuploadMedia("Image url"); if($mediaID > 0) { $aProduct->set_image_id($mediaID); } $product_id = $aProduct->save(); function wpsuploadMedia($aImage) { require_once('wp-admin/includes/image.php'); require_once('wp-admin/includes/file.php'); require_once('wp-admin/includes/media.php'); $media = media_sideload_image($aImage,0); $attachments = get_posts(array( 'post_type' => 'attachment', 'post_status' => null, 'post_parent' => 0, 'orderby' => 'post_date', 'order' => 'DESC' )); return $attachments[0]->ID; }