Skip to content

Instantly share code, notes, and snippets.

@codetycon
Created August 21, 2018 17:32
Show Gist options
  • Save codetycon/f7cf60c25d72b12e1c061c7bab61b1b4 to your computer and use it in GitHub Desktop.
Save codetycon/f7cf60c25d72b12e1c061c7bab61b1b4 to your computer and use it in GitHub Desktop.
How to add a product thumbnail and product galery from a images URL using php script in WordPress Woocommerce.
<?php
function downloadAndUploadImages($post_id,$post_title,$img,$sku){
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
ini_set('memory_limit','1024M');
$ext = substr(strrchr($img,'.'),1);
// WordPress Upload Directory to Copy to (must be CHMOD to 777)
$uploads = wp_upload_dir();
$copydir = $uploads['path']."/";
// Code to Copy Image to WordPress Upload Directory (Server Must Support file_get_content/fopen/fputs)
$data = @file_get_contents($img);
$filetitle = strtolower(str_replace(array(' ', '-', '.', '(', ')', '!', '@', '#', '$', '%', '^', '&', '*', '_', '=', '+', '/', '"'), "-", $post_title));
$file = fopen($copydir . "$filetitle-$sku-$post_id.$ext", "w+");
fputs($file, $data);
fclose($file);
// Insert Image to WordPress Media Libarby
$filepath = $uploads['path']."/$filetitle-$sku-$post_id.$ext";
$wp_filetype = wp_check_filetype(basename($filepath), null );
$attachment = array(
'guid' =>$filepath,
'post_mime_type' => $wp_filetype['type'],
'post_title' => $post_title,
'post_content' => 'Image for '.$post_title,
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment( $attachment, $filepath, $post_id);
// Include image.php
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attachment_id, $filepath );
// Assign metadata to attachment
wp_update_attachment_metadata( $attachment_id, $attach_data );
return $attachment_id;
}
//How to use this function
$product_id = 1;
$sku = 'ASDF';
$product_title = 'Test Product';
/#1 For product thumbnail
$thumbnail_url = 'http://placehold.it/120x120&text=image1';
$attachment_id = downloadAndUploadImages($product_id,$product_title,$thumbnail_url,'Thumbnail-'.$sku ); // We adding Thumbnail to sku for providing a unique name to each image on Wordpress
add_post_meta($product_id, '_thumbnail_id', $attachment_id);
//#2 For galery
$images = array(
'http://placehold.it/120x120&text=image1',
'http://placehold.it/120x120&text=image2',
'http://placehold.it/120x120&text=image3',
'http://placehold.it/120x120&text=image4'
);
$prd_img_galery = array();
$count = 1;
foreach($images as $image){
$attachment_id = downloadAndUploadImages($product_id,$product_title,$image,$count.'-'.$sku ); // We adding $count to sku for providing a unique name to each image on Wordpress
$prd_img_galery[] = $attachment_id;
$count ++;
}
if(count($prd_img_galery)>1){
update_post_meta($product_id, "_product_image_gallery", implode(',', $prd_img_galery));
}elseif(count($prd_img_galery)==1){
update_post_meta($product_id, "_product_image_gallery",trim($prd_img_galery[0]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment