programing

Wocommerce Rest API : wocommerce_product_invalid_image_id

lastmoon 2023. 9. 14. 23:25
반응형

Wocommerce Rest API : wocommerce_product_invalid_image_id

우커머스 버전: 4.1.0

API를 통해 카테고리와 이미지로 제품을 만들려고 하는데 다음과 같은 오류가 발생합니다.

{"code":"wocommerce_product_invalid_image_id", 메시지:"#0은 잘못된 이미지 ID입니다.", 데이터":{"상태":400}}

몇가지 답변을 확인했지만 url에서 image extension을 사용하고 id가 아닌 url만 사용하고 W3Cache plugin을 사용하지 않습니다.다음은 요청의 이미지 배열입니다.

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => https://www.domainzzz.com/wp-content/uploads/2019/05/MB-150x150.jpg
                    [1] => 150
                    [2] => 150
                    [3] => 1
                )

            [position] => 0
        )

    [1] => Array
        (
            [0] => https://www.domainzzz.com/wp-content/uploads/2020/04/MG_2072-copy.png
            [position] => 1
        )

    [2] => Array
        (
            [0] => https://www.domainzzz.com/wp-content/uploads/2020/04/MG_2071-copy.png
            [position] => 2
        )

)

그리고 이것이 완전한 요청입니다.

$api_response = wp_remote_post( 'https://newdomain.com/wp-json/wc/v2/products', [
        'headers' => [
            'Authorization' => 'Basic ' . base64_encode( 'ck_306dd02a04f81cc33df:cs_4b3c7be0a0' )
        ],
        'body' => [
            'name' => $product->get_name(), // product title
            'slug' => $product->get_slug(),
            'sku' => $product->get_sku(),
            'description' => $product->get_description(),
            'status' => get_post_status($post_id), // product status, default: publish
            'categories' => $cat_ids,
            'images' => $images,
            'regular_price' => $product->get_price() // product price
            // more params http://woocommerce.github.io/woocommerce-rest-api-docs/?shell#product-properties
        ]
    ] );

이미지 배열의 올바른 형식은 다음과 같습니다.

$images = array(
    array(
        'src' => 'https://www.domainzzz.com/wp-content/uploads/2019/05/MB-150x150.jpg',
        'position' => 0
    ),
    array(
        'src' => 'https://www.domainzzz.com/wp-content/uploads/2020/04/MG_2072-copy.png',
        'position' => 1
    )
)
$api_response = wp_remote_post('https://newdomain.com/wp-json/wc/v2/products', [
    'headers' => [
        'Authorization' => 'Basic ' . base64_encode('ck_306dd02a04f81cc33df:cs_4b3c7be0a0')
    ],
    'body' => [
        'name' => $product->get_name(), // product title
        'slug' => $product->get_slug(),
        'sku' => $product->get_sku(),
        'description' => $product->get_description(),
        'status' => get_post_status($post_id), // product status, default: publish
        'categories' => [
            [
                'id' => 9
            ],
            [
                'id' => 14
            ]
        ],
        'images' => [
            [
                'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg'
            ],
            [
                'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg'
            ]
        ],
        'regular_price' => $product->get_price() // product price
        // more params http://woocommerce.github.io/woocommerce-rest-api-docs/?shell#product-properties
    ]
]);

언급URL : https://stackoverflow.com/questions/61793909/woocommerce-rest-api-woocommerce-product-invalid-image-id

반응형