Woocommerce通过php更改产品图像

Woocommerce通过php更改产品图像,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在尝试通过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

我正在尝试通过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不熟悉)

您正在尝试设置特色图像吗

如果是这样,您可能希望使用中给出的函数

功能:

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()
用法:

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()

我想补充一下布莱斯·亚当斯的答案

例如,eBay的Finding API返回的图像的URL结构不允许使用该命令

$filename = basename($image_url);
按预期操作,因为发生的情况是,图像的所有URL都具有相同的结束基名称,并且文件会发生冲突。我发现这个命令

$filename = basename($image_url, '.jpg') . (string) rand(0, 5000) . '.jpg';
对于许多API来说效果更好。希望它能起到作用