duanfei8897 2018-07-31 05:11
浏览 158

使用新缩略图PHP设置Woocommerce产品缩略图

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.

  • 写回答

1条回答 默认 最新

  • doukuang8166 2018-07-31 05:57
    关注

    I found the bug. The first query was returning all the matching result and as picked the first post id so, it returns the first matching post id. The code should be like:

    $product_img = $row[13]; //Product Image
    
        $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 );
    

    This works fine.

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置