Set Woocommerce product thumbnail with new thumbnail PHP
Set Woocommerce product thumbnail with new thumbnail PHP
I have created a custom plugin to import and export woocommerce products. I am facing an issue that when i try to import the CSV, the thumbnails of the products got changed and set to the old one which were added to the products earlier.
I have full image URL in the CSV. Here is my import code:
$product_img = $row[13]; //Product Image
$res = $wpdb->get_results('select post_id from ' . $wpdb->prefix . 'postmeta where meta_value like "%' . basename($product_img). '%"');
if(count($res) == 0)
{
$res = $wpdb->get_results('select ID as post_id from ' . $wpdb->prefix . 'posts where guid="' . $product_img . '"');
}
$thumbnail_id = $res[0]->post_id;
set_post_thumbnail( $product_id, $thumbnail_id );
$attach_data = wp_generate_attachment_metadata( $thumbnail_id, $product_img );
wp_update_attachment_metadata( $thumbnail_id, $attach_data );
Can anyone tell me where I am wrong and why the thumbnails are updating with the old one. I am trying to find the issue from last 1 day but didn't found it yet.
Thanks in advance.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.