<?php
require_once("wp-load.php");
$post_id = wp_insert_post(array(
'post_title' => 'Test Product'.rand(10,100),
'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 industrys 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 industrys 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.'
));
// set product category
$term_ids = [ 343, 114, 115 ];
wp_set_object_terms( $post_id, $term_ids, 'product_cat' );
// set product is simple/variable/grouped
wp_set_object_terms( $post_id, 'variable', 'product_type' );
update_post_meta( $post_id, '_visibility', 'visible' );
// update_post_meta( $post_id, 'total_sales', '0' );
// update_post_meta( $post_id, '_downloadable', 'no' );
// update_post_meta( $post_id, '_virtual', 'no' );
// update_post_meta( $post_id, '_regular_price', '20' );
// 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', '11' );
// 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', 'SKU11'.rand(10,100));
// update_post_meta( $post_id, '_sale_price_dates_from', '' );
// update_post_meta( $post_id, '_sale_price_dates_to', '' );
// update_post_meta( $post_id, '_price', '11' );
// update_post_meta( $post_id, '_sold_individually',"" );
// update_post_meta( $post_id, '_manage_stock', 'yes' );
// update_post_meta( $post_id, '_backorders', 'no' );
// update_post_meta( $post_id, '_price', '156' );
// update_post_meta( $post_id, '_featured', 'yes' );
// update_post_meta( $post_id, '_stock', '23' );
// update_post_meta( $post_id, '_stock_status', 'instock');
// update_post_meta( $post_id, '_sku', 'jk01' );
//set product feature image
attach_product_thumbnail($post_id, 'https://static.reimo-shops.de/article-imagepool/full/9c/fa/21003.jpg', 0);
$single_image = array(
array('url_original' => 'https://www.nncinfotech.com/wp-content/uploads/2018/06/main.jpg'),
array('url_original' => 'https://www.nncinfotech.com/wp-content/uploads/2018/06/main-66.jpg'),
array('url_original' => 'https://www.nncinfotech.com/wp-content/uploads/2018/02/portfolio005.jpg'),
);
$attr_label = 'Size';
$attr_slug = sanitize_title($attr_label);
$attr_label2 = 'color';
$attr_slug2 = sanitize_title($attr_label2);
$attributes_array[$attr_slug2] = array(
'name' => $attr_label2,
'value' => 'Green | Blue',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '0' // for some reason, this is really important
);
$attributes_array[$attr_slug] = array(
'name' => $attr_label,
'value' => 'L|M',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '0' // for some reason, this is really important
);
update_post_meta( $post_id, '_product_attributes', $attributes_array );
$parent_id = $post_id;
$variation = array(
'post_title' => $article_name . '(variation)',
'post_content' => '',
'post_status' => 'publish',
'post_parent' => $parent_id,
'post_type' => 'product_variation'
);
$variation_id2 = wp_insert_post( $variation2 );
update_post_meta( $variation_id2, '_regular_price', 200 );
update_post_meta( $variation_id2, '_price',199 );
update_post_meta( $variation_id2, '_stock', '23' );
update_post_meta( $variation_id2, 'attribute_' .$attr_slug2, 'Green' );
$variation_id = wp_insert_post( $variation );
update_post_meta( $variation_id, '_regular_price', 2 );
update_post_meta( $variation_id, '_price', 2 );
update_post_meta( $variation_id, '_stock',20);
update_post_meta( $variation_id, 'attribute_' . $attr_slug, 'L' );
$variation_id = wp_insert_post( $variation );
update_post_meta( $variation_id, '_regular_price', 2 );
update_post_meta( $variation_id, '_price', 2 );
update_post_meta( $variation_id, '_stock', '10');
update_post_meta( $variation_id, 'attribute_' . $attr_slug, 'Blue' );
WC_Product_Variable::sync( $parent_id );
$variation_id = wp_insert_post( $variation );
update_post_meta( $variation_id, '_regular_price', 2 );
update_post_meta( $variation_id, '_price', 2 );
update_post_meta( $variation_id, '_stock_quantity', 10 );
update_post_meta( $variation_id, 'attribute_'.$attr_slug, 'M' );
WC_Product_Variable::sync( $parent_id );
foreach($single_image as $screenshots){
//set gallery image
attach_product_thumbnail($post_id, $screenshots['url_original'], 1);
}
function attach_product_thumbnail($post_id, $url, $flag){
$image_url = $url;
$url_array = explode('/',$url);
$image_name = $url_array[count($url_array)-1];
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $image_url);
// Getting binary data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$image_data = curl_exec($ch);
curl_close($ch);
$upload_dir = wp_upload_dir(); // Set upload folder
$unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name
$filename = basename( $unique_file_name ); // Create image file name
// Check folder permission and define file location
if( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
// Create the image file on the server
file_put_contents( $file, $image_data );
// Check image file type
$wp_filetype = wp_check_filetype( $filename, null );
// Set attachment data
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
// Create the attachment
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
// Include image.php
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id, $attach_data );
// asign to feature image
if( $flag == 0){
// And finally assign featured image to post
set_post_thumbnail( $post_id, $attach_id );
}
// assign to the product gallery
if( $flag == 1 ){
// Add gallery image to product
$attach_id_array = get_post_meta($post_id,'_product_image_gallery', true);
$attach_id_array .= ','.$attach_id;
update_post_meta($post_id,'_product_image_gallery',$attach_id_array);
}
}
die();
0 Comments