Add product in woocommerce with php code

How to add product in  Woocommerce in php  programmatically? Sometimes you need your own product importer. Now you got all your products in memory or cache or your temporary file and what’s next? You need to add this product to Woocommerce With PHP Code

Well, the product is – like almost anything in WordPress – post type with specific taxonomy and meta data set. So, first things first, insert your new post:

Of course this is not everything, that would be too easy. Now we need to add some meta information to the product post and that is why, we take care of our new posts ID.

his will relate our post as a simple product. Please be careful, because this function will create term/taxonomy if those doesn’t exists. First you must know you are running WordPress with Woocommerce enabled.
Finally, we need to setup some meta informations. Some of them are important, some of them doesn’t, but it is always a good practice to set it up. We need to use update_post_meta function.

<?php
Array Like This
$products = Array
(
[0] => stdClass Object
(
[product_id] => 432116
[product_title] => 925 silver sterling opal pearl chalcedony citrine deltoid leaf pendant k27271
[product_price] => 22.99
[product_image] => https://www.webtechsource.com/925-silver-sterling-opal-pearl-chalcedony-citrine-deltoid-leaf-pendant-k27271.JPG
[product_url] => https://www.webtechsource.com/925-silver-sterling-opal-pearl-chalcedony-citrine-deltoid-leaf-pendant-k27271
[category] => pendants
[description] =>
Model #: KP-27271
Metal Type: 925 SILVER
Style: Pendant
Stone Weight: 13.04
size: (inch): 2
Primary Stone: STERLING OPAL
Color: MULTICOLOR
Shape: Oval
Weight (Gms): 5.67
Secondary Stone: MULTI GEMSTONE

)
echo “<pre />”;
print_r($products);
if(count($products)>0)
{

foreach($products as $product)
{
$product_name = $product->product_title;
$product_price = $product->product_price;
$product_image = $product->product_image;
$product_url = $product->product_url;
$category = $product->category;
$description = $product->description;
$user_id = get_current_user_id();

$post = array(
‘post_author’ => $user_id,
‘post_content’ => ”,
‘post_status’ => “publish”,
‘post_title’ => $product_name ,
‘post_content’ =>$description,
‘post_parent’ => ”,
‘post_type’ => “product”,
);

//Create post
$post_id = wp_insert_post( $post, $wp_error );
if($post_id){
$attach_id = get_post_meta($product->parent_id, “_thumbnail_id”, true);
add_post_meta($post_id, ‘_thumbnail_id’, $attach_id);
}

wp_set_object_terms( $post_id, $category , ‘product_cat’ );
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’, ‘yes’);
update_post_meta( $post_id, ‘_virtual’, ‘yes’);
update_post_meta( $post_id, ‘_regular_price’, $product_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’, “”);
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’, $product_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’, “” );
update_post_meta( $post_id, ‘_product_image_gallery’, $product_image );

// Code for product image upload

$image_url = $product_image;

$upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
$filename = basename($image_url);
if (wp_mkdir_p($upload_dir[‘path’]))
$file = $upload_dir[‘path’] . ‘/’ . $filename;
else
$file = $upload_dir[‘basedir’] . ‘/’ . $filename;
file_put_contents($file, $image_data);

$wp_filetype = wp_check_filetype($filename, null);
$attachment = array(
‘post_mime_type’ => $wp_filetype[‘type’],
‘post_title’ => sanitize_file_name($filename),
‘post_content’ => ”,
‘post_status’ => ‘inherit’
);
$attach_id = wp_insert_attachment($attachment, $file, $post_id);
require_once(ABSPATH . ‘wp-admin/includes/image.php’);
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
$res1 = wp_update_attachment_metadata($attach_id, $attach_data);
$res2 = set_post_thumbnail($post_id, $attach_id);

}
}
?>

I, Dayanand Saini,the founder and chief editor of www.webtechsource.com,have been working as a software engineer from last 4+ years. It's my hobby to learn new things and implement on live environment.

Leave a Reply

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

72 − 64 =

%d bloggers like this: