My project requirement is pulling an image from remote server and with that image create a product with size attribute of 5 sizes lets say 10X10, 20X20, 30X30, 40X40, 50X50 set pulled image to product and redirect to that product details page.
here is the URL of buy button by clicking where you redirect to your sites respective page:
yoursiteurl?img=your_image_url&cap=your_caption&cat=your_category&isrc=image_source_site
Here is my code:
$caption = isset($_GET['cap']) ? $_GET['cap'] : '';
$url_category = isset($_GET['cat']) ? $_GET['cat'] : '';
$image_source = isset($_GET['isrc']) ? $_GET['isrc'] : '';
$diff_paths = wp_upload_dir();
$saveto = $diff_paths['path'] . '/' . md5(rand(0, 1000)) . time() . '.jpg';
$image = $_GET['img'];
try {
$ch = curl_init($image);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$raw = curl_exec($ch);
if ($raw === false) {
echo "<h3>Something went wrong, kindly try again later. Error :" . curl_error($ch) . " </h3>";
die();
} else
if (file_exists($saveto)) {
unlink($saveto);
}
curl_close($ch);
$fp = fopen($saveto, 'x');
fwrite($fp, $raw);
fclose($fp);
} catch (Exception $e) {
echo $e . getMessage();
die();
}
$prod_info = addaprdct_to_woo($saveto, $caption, $url_category);
$wpdb->insert('wp_product_imageurl_mapping', array(
'product_id' => $prod_info['prod_id'],
'image_url' => urlencode($_GET['img']),
'image_source' => urlencode($image_source)
));
ob_start();
header("location:" . $prod_info['prod_url']);
ob_end_flush();
die();
function addaprdct_to_woo($prod_img,$caption,$url_category) {
global $wpdb;
$insertLog = "insert_product_logs.txt"; //name the log file in wp-admin folder
$post = array();
$post = array(
'post_title' => "TB#".time(),
'post_content' => $caption,
'post_status' => "publish",
// 'post_excerpt' => "product excerpt content...",
'post_name' => "TB#".time(), //name/slug
'post_type' => "product"
);
//Create product/post:
$new_post_id = wp_insert_post($post, $wp_error);
$logtxt = "PrdctID: $new_post_id\n";
//make product type be variable:
wp_set_object_terms($new_post_id, 'variable', 'product_type');
//add category to product:
wp_set_object_terms($new_post_id, $url_category, 'product_cat');
//################### Add size attributes to main product: ####################
//Array for setting attributes
$avail_attributes = array(
'8"x10"',
'16"x20"',
'20"x24"',
'20"x30"',
'24"x24"',
'36"x48"'
);
wp_set_object_terms($new_post_id, $avail_attributes, 'pa_size');
$thedata = Array('pa_size' => Array(
'name' => 'pa_size',
'value' => '',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
));
update_post_meta($new_post_id, '_product_attributes', $thedata);
//########################## Done adding attributes to product #################
//set product values:
update_post_meta($new_post_id, '_stock_status', 'instock');
// update_post_meta($new_post_id, '_weight', "0.06");
// update_post_meta($new_post_id, '_sku', "skutest1");
update_post_meta($new_post_id, '_stock', "100");
update_post_meta($new_post_id, '_visibility', 'visible');
//###################### Add Variation post types for sizes #############################
//insert 5 variations post_types for 8"x10", 16"x20", 20"x30", 20"x24", 24"x24":
$i = 1;
while ($i <= 6) {//while creates 5 posts(1 for ea. size variation 8"x10", 16"x20" etc):
$my_post = array(
'post_title' => 'Variation #' . $i . ' of 6 for prdct#' . $new_post_id,
'post_name' => 'product-' . $new_post_id . '-variation-' . $i,
'post_status' => 'publish',
'post_parent' => $new_post_id, //post is a child post of product post
'post_type' => 'product_variation', //set post type to product_variation
'guid' => home_url() . '/?product_variation=product-' . $new_post_id . '-variation-' . $i
);
//Insert ea. post/variation into database:
$attID = wp_insert_post($my_post);
$logtxt .= "Attribute inserted with ID: $attID\n";
//set IDs for product_variation posts:
$variation_id = $new_post_id + 1;
$variation_two = $variation_id + 1;
$variation_three = $variation_two + 1;
$variation_four = $variation_three + 1;
$variation_five = $variation_four + 1;
$variation_six = $variation_five + 1;
//Create 8"x10" variation for ea product_variation:
update_post_meta($variation_id, 'attribute_pa_size', '8x10');
update_post_meta($variation_id, '_price', 29.49);
update_post_meta($variation_id, '_regular_price', '29.49');
//add size attributes to this variation:
wp_set_object_terms($variation_id, $avail_attributes, 'pa_size');
$thedata = Array('pa_size' => Array(
'name' => '8x10',
'value' => '',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
));
update_post_meta($variation_id, '_product_attributes', $thedata);
//Create 16"x20" variation for ea product_variation:
update_post_meta($variation_two, 'attribute_pa_size', '16x20');
update_post_meta($variation_two, '_price', 39.49);
update_post_meta($variation_two, '_regular_price', '39.49');
//add size attributes:
wp_set_object_terms($variation_two, $avail_attributes, 'pa_size');
$thedata = Array('pa_size' => Array(
'name' => '16x20',
'value' => '',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
));
update_post_meta($variation_two, '_product_attributes', $thedata);
//Create 20"x30" variation for ea product_variation:
update_post_meta($variation_three, 'attribute_pa_size', '20x30');
update_post_meta($variation_three, '_price', 59.49);
update_post_meta($variation_three, '_regular_price', '59.49');
wp_set_object_terms($variation_three, $avail_attributes, 'pa_size');
$thedata = Array('pa_size' => Array(
'name' => '20x30',
'value' => '',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
));
update_post_meta($variation_three, '_product_attributes', $thedata);
//Create 20"x24" variation for ea product_variation:
update_post_meta($variation_four, 'attribute_pa_size', '20x24');
update_post_meta($variation_four, '_price', 49.49);
update_post_meta($variation_four, '_regular_price', '49.49');
wp_set_object_terms($variation_four, $avail_attributes, 'pa_size');
$thedata = Array('pa_size' => Array(
'name' => '20x24',
'value' => '',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
));
update_post_meta($variation_four, '_product_attributes', $thedata);
//Create 24"x24" variation for ea product_variation:
update_post_meta($variation_five, 'attribute_pa_size', '24x24');
update_post_meta($variation_five, '_price', 69.49);
update_post_meta($variation_five, '_regular_price', '69.49');
wp_set_object_terms($variation_five, $avail_attributes, 'pa_size');
$thedata = Array('pa_size' => Array(
'name' => '24x24',
'value' => '',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
));
update_post_meta($variation_five, '_product_attributes', $thedata);
//Create 24"x24" variation for ea product_variation:
update_post_meta($variation_six, 'attribute_pa_size', '36x48');
update_post_meta($variation_six, '_price', 99.49);
update_post_meta($variation_six, '_regular_price', '99.49');
wp_set_object_terms($variation_six, $avail_attributes, 'pa_size');
$thedata = Array('pa_size' => Array(
'name' => '36x48',
'value' => '',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
));
update_post_meta($variation_six, '_product_attributes', $thedata);
$i++;
}//end while i is less than or equal to 5(for 5 size variations)
//############################ Done adding variation posts ############################
//add product image:
Generate_Featured_Image($prod_img, $new_post_id);
$prod_info = array();
$prod_info['prod_id'] = $new_post_id;
$prod_info['prod_url'] = get_permalink($new_post_id);
return $prod_info;
}
function Generate_Featured_Image($image_url, $post_id) {
$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);
}
Now the problem is this whole process starting from buy button click to product details page is very time consuming it takes around 12 - 20 seconds depends upon size of image and Internet speed, avg time is 15 seconds which is very large. How can this code be optimized so that it should not take this much of time to have product details page(ie. view product page).
Thanks in advance.