最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Woocommerce api: create product with images - bad request

programmeradmin2浏览0评论

I am trying to create a product with images via the Woocommerce Rest API but I get bad request:

Here is how I am preparing the images array:

//IMAGES
    $images = [];
    //Get the featured image
    $featured_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post_id));
    //and push it in the images array with position = 0 (featured)
    array_push($images, ['src' => $featured_image_url[0], 'position' => 0]);
    //Get All the images
    $attachmentIds = $product->get_gallery_attachment_ids();
    $pos = 1;
    foreach( $attachmentIds as $attachmentId )
    {   
        array_push($images, ['src' => wp_get_attachment_image_src( $attachmentId ), 'position' => $pos]);
        $pos++;
    }

This is code is being called after a product is published, and when I am publishing a product with featured image and 2 more images in its gallery I get this as the $images array:

Array
(
    [0] => Array
        (
            [src] => 
            [position] => 0
        )

)

I get only the featured image, and I receive bad request when I send this post request to create the product. If I comment out the images attribute of the request then the product is created successfully.

发布评论

评论列表(0)

  1. 暂无评论