首页 > 解决方案 > Woocommerce api:使用图像创建产品 - 错误请求

问题描述

我正在尝试通过创建带有图像的产品,Woocommerce Rest API但我得到bad request

这是我准备的方法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++;
    }

这是在产品发布后调用的代码,当我在其图库中发布具有特色图像和另外 2 个图像的产品时,我将其作为$images数组:

Array
(
    [0] => Array
        (
            [src] => http://src_image-150x150.jpg
            [position] => 0
        )

)

我只得到特色图片,并且bad request在我发送此帖子请求以创建产品时收到。如果我注释掉images请求的属性,则产品创建成功。

标签: phpwordpresswoocommercewoocommerce-rest-api

解决方案


推荐阅读