Try like
$post_id = wp_insert_post( array(
'post_title' => $items['description'],
'post_content' => $items['detail'],
'post_excerpt' => $items['description_2'],
'post_status' => 'publish',
'post_type' => "product",
) );
// Set Product type
// wp_set_object_terms( $post_id, 'simple', 'product_type');
wp_set_post_terms( $post_id, 'simple', 'product_type');
// Get parent category returns mixed or objects
$category_parent = get_term_by('name', 'Lense', 'product_cat');
// Get child category returns array
$category_child = term_exists( 'Canon', 'product_cat', $category_parent->term_id);
// Set product brand
// wp_set_object_terms( $post_id, 'Canon', 'product_brand');
// wp_set_post_terms( $post_id, 'Canon', 'product_brand');
// Set category for this product post
// wp_set_object_terms( $post_id, $category_parent->term_id, 'product_cat', false);
// wp_set_object_terms( $post_id, (int)$category_child['term_id'], 'product_cat', true);
wp_set_post_terms($post_id, array($category_parent->term_id,(int)$category_child['term_id']), 'product_cat');
// add_post_meta( $post_id, '_wc_booking_availability', $availability );
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', "1" );
update_post_meta( $post_id, '_sale_price', "1" );
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', "1" );
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', "" );
// ===================== Create product image =================
$image = $items['cover_img_url'];
// magic sideload image returns an HTML image, not an ID
$media = media_sideload_image($image, $post_id);
// therefore we must find it so we can set it as featured ID
if(!empty($media) && !is_wp_error($media)){
$args = array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_status' => 'any',
'post_parent' => $post_id
);
// reference new image to set as featured
$attachments = get_posts($args);
if(isset($attachments) && is_array($attachments)){
foreach($attachments as $attachment){
// grab source of full size images (so no 300x150 nonsense in path)
$image = wp_get_attachment_image_src($attachment->ID, 'full');
// determine if in the $media image we created, the string of the URL exists
if(strpos($media, $image[0]) !== false){
// if so, we found our image. set it as thumbnail
set_post_thumbnail($post_id, $attachment->ID);
// only want one image
break;
}
}
}
}
// ================= Create Product Gallery
$images = $items['images'];
if(isset($images) && is_array($images)){
$list_id = "";
// magic sideload image returns an HTML image, not an ID
$media = media_sideload_image($images[1]['full_url'], $post_id);
// therefore we must find it so we can set it as featured ID
if(!empty($media) && !is_wp_error($media)){
$args = array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_status' => 'any',
'post_parent' => $post_id
);
// reference new image to set as featured
$attachments = get_posts($args);
if(isset($attachments) && is_array($attachments)){
foreach($attachments as $attachment){
// grab source of full size images (so no 300x150 nonsense in path)
$image = wp_get_attachment_image_src($attachment->ID, 'full');
// determine if in the $media image we created, the string of the URL exists
if(strpos($media, $image[0]) !== false){
// if so, we found our image. set it as thumbnail
// set_post_thumbnail($post_id, $attachment->ID);
$list_id .= $attachment->ID . ",";
// only want one image
break;
}
}
}
}
update_post_meta($post_id,'_product_image_gallery',$list_id);
Hope it will help !