【问题标题】:Set Woocommerce product thumbnail with new thumbnail PHP使用新的缩略图 PHP 设置 Woocommerce 产品缩略图
【发布时间】:2019-01-07 09:41:35
【问题描述】:

我创建了一个自定义插件来导入和导出 woocommerce 产品。我面临一个问题,当我尝试导入 CSV 时,产品的缩略图被更改并设置为之前添加到产品中的旧缩略图。

我在 CSV 中有完整的图片网址。这是我的导入代码:

    $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 );

谁能告诉我我错在哪里以及为什么缩略图会用旧的更新。我正在尝试查找过去 1 天的问题,但尚未找到。

提前致谢。

【问题讨论】:

    标签: php wordpress woocommerce thumbnails product


    【解决方案1】:

    我发现了错误。第一个查询返回所有匹配的结果,并且选择了第一个帖子 ID,因此它返回第一个匹配的帖子 ID。代码应该是这样的:

    $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 );
    

    这很好用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      • 2018-09-30
      • 2023-03-22
      • 2017-05-24
      • 2011-10-16
      • 2019-02-09
      • 2014-02-24
      相关资源
      最近更新 更多