//unset($gallery[0]);
$gallery_urls = array();
foreach($gallery as $one_picture){
//$source = explode('src="', $one_picture)[1];
$source = explode('"', $one_picture)[0];
if(strpos($source, '/images/products/originals')>-1){
$gallery_urls[] = '/uploads'.$source;
}
}
$post = array(
'post_content' => $description,
'post_status' => "publish",
'post_title' => $name,
'post_type' => "product",
);
//Create post
$post_id = wp_insert_post( $post, $wp_error );
echo "
";
update_post_meta( $post_id, 'original_url',$product_url);
update_post_meta( $post_id, 'size_guide', $size_guide);
$sku = explode('/item/', $product_url)[1];
$sku = explode('/', $sku)[0];
update_post_meta( $post_id, '_sku', $sku);
update_post_meta( $post_id, '_product_attributes', array());
update_post_meta( $post_id, '_regular_price',$price );
if($sale_price>0){
update_post_meta( $post_id, '_sale_price', $sale_price );
}
wp_set_object_terms( $post_id, 'variable', 'product_type' );
echo "
";
$images = array(); //var_dump(strip_tags($gallery));
$i = 0;
$product_gallery = array();
foreach($gallery_urls as $image){
$image = 'https://www.manifesto-clothing.co.uk/'.$image;
$image_url = $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 );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
$product_gallery[] = $attach_id;
wp_update_attachment_metadata( $attach_id, $attach_data );
if($i==0){
add_post_meta($post_id, '_thumbnail_id', $attach_id);
}
$i++;
}
unset($product_gallery[0]);
var_dump(join(',', $product_gallery));
update_post_meta($post_id, '_product_image_gallery', join(',', $product_gallery));
var_dump($images);
echo "
";
$the_data = array();
$attributes = $the_sizes;
// unset(count($oneAttribute)); // last empty
$attributeName = "Size";
//$attributeName = $attData[0];
/*$attributeName = explode('<', $attributeName)[0];
$attributeName = rtrim(($attributeName));
$attributeName = ltrim($attributeName);
$attributeName = trim($attributeName);*/
$product_id = $post_id;
var_dump($variableAttributes);
var_dump($attributeName);
echo "test";
$fragrance_array = Array(
'name'=> $attributeName,
'value'=> join('|', $the_sizes),
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '0'
);
$attributeName = sanitize_title($attributeName);
wp_set_object_terms( $post_id, $the_sizes, 'pa_'.$attributeName );
$the_data[$attributeName]=$fragrance_array;
$result = update_post_meta( $post_id,'_product_attributes',$the_data);
wp_set_object_terms( $post_id, $product_category, 'product_cat' );
wp_set_object_terms( $post_id, $product_brand, 'pwb-brand' );
wp_set_object_terms( $post_id, $product_brand, 'yith_product_brand' );
var_dump($result);
echo "trying variation";
$parent_id = $post_id;
$variation = array(
'post_title' => $name . ' (variation)',
'post_content' => '',
'post_status' => 'publish',
'post_parent' => $parent_id,
'post_type' => 'product_variation'
);
$variation_id = wp_insert_post( $variation );
update_post_meta( $variation_id, '_regular_price', $price );
if($sale_price>0){
update_post_meta( $variation_id, '_sale_price', $sale_price );
}
update_post_meta( $variation_id, '_stock_qty', 100000 );
WC_Product_Variable::sync( $parent_id );
echo "
";
}
}
function removeBOM($data) {
if (0 === strpos(bin2hex($data), 'efbbbf')) {
return substr($data, 3);
}
return $data;
}
?>
Author: Wojciech Borowicz
I hope this post will help you to do what you need.
In case you want some assistance click here to get in touch