【问题标题】:Woocommerce change product image via phpWoocommerce 通过 php 更改产品图片
【发布时间】:2017-08-07 17:02:20
【问题描述】:

我正在尝试通过 php 更改产品图片。但我无法让它工作。

    require_once(ABSPATH . 'wp-admin/includes/media.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    $yesterday = date("Y-m-d",  time() - 60 * 60 * 24);
    $jsondata = file_get_contents("730.json");

    ini_set('memory_limit', '-1');
    $json = json_decode($jsondata, true);
    $price = $json["AWP | Asiimov (Well-Worn)"][$yesterday]["price"]/100;

    $post_information = array(
        'post_author' => $user_id,
        'post_content' => '',
        'post_status' => "publish",
        'post_title' => 'test',
        'post_parent' => '',
        'post_type' => "product",
    );

    $image = media_sideload_image("http://www.technologijos.lt/upload/image/n/technologijos/it/S-49657/samsung-ssd-840-evo.jpg", 0, "The WordPress Logo");

    $post_id = wp_insert_post($post_information);

    $attachment = wp_get_attachment_image( 18, 'thumbnail' );

    update_post_meta( $post_id, '_visibility', 'visible' );
    update_post_meta( $post_id, '_stock_status', 'instock');
    update_post_meta( $post_id, 'total_sales', '0');
    update_post_meta( $post_id, '_downloadable', 'no');
    update_post_meta( $post_id, '_virtual', 'yes');
    update_post_meta( $post_id, '_regular_price', $price );
    update_post_meta( $post_id, '_sale_price', "1" );
    update_post_meta( $post_id, '_purchase_note', "" );
    update_post_meta( $post_id, '_featured', "no" );
    update_post_meta($post_id, '_sku', "145");
    update_post_meta( $post_id, '_product_attributes', array());
    update_post_meta( $post_id, '_sale_price_dates_from', "" );
    update_post_meta( $post_id, '_sale_price_dates_to', "" );
    update_post_meta( $post_id, '_price', $price );
    update_post_meta( $post_id, '_sold_individually', "" );
    update_post_meta( $post_id, '_manage_stock', "no" );
    update_post_meta( $post_id, '_backorders', "no" );
    update_post_meta( $post_id, '_stock', "" );
    update_post_meta( $post_id,  '_product_image_gallery', $attachment  );

最后一行不起作用。而且我不知道如何让它工作。我尝试改变不同的东西,(我是 PHP/WordPress 的新手)。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您要设置特色图片吗?

    如果是这样,您可能需要使用answer here 中给出的函数。

    功能:

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

    用法:

    Generate_Featured_Image( '../wp-content/my_image.jpg',   $post_id );
                                                          // $post_id is Numeric ID... You can also get the ID with:          wp_insert_post()
    

    【讨论】:

      【解决方案2】:

      我想添加到 bryceadams 的答案。

      好像是这样,例如,eBay 的 Finding API 返回的图像的 URL 具有不允许该命令的结构

      $filename = basename($image_url);
      

      按预期运行,因为发生的情况是,图像的所有 URL 都具有相同的结尾基本名称,并且文件会发生冲突。我发现命令

      $filename = basename($image_url, '.jpg') . (string) rand(0, 5000) . '.jpg';
      

      适用于许多 API。希望它加起来并有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-16
        • 1970-01-01
        • 1970-01-01
        • 2020-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多