1

I'm trying to create a new product with multiple images using WooCommerce REST API

Here is the REST API syntax for images src: https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-product

'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'
    ]
]

Now on PHP I have images URLs in an array $images like this:

Array
(
    [0] => https://images-na.ssl-images-amazon.com/images/I/61imqvdVv1L._SL1000_.jpg
    [1] => https://images-na.ssl-images-amazon.com/images/I/61CpVDq9iwL._SL1000_.jpg
    [2] => https://images-na.ssl-images-amazon.com/images/I/61IMXxbcfpL._SL1000_.jpg
    [3] => https://images-na.ssl-images-amazon.com/images/I/61HFUejnppL._SL1000_.jpg
    [4] => https://images-na.ssl-images-amazon.com/images/I/51whKF45l0L._SL1000_.jpg
    [5] => https://images-na.ssl-images-amazon.com/images/I/51uPvXbo3IL._SL1000_.jpg
    [6] => https://images-na.ssl-images-amazon.com/images/I/61sNz9zDalL._SL1000_.jpg
    [7] => https://images-na.ssl-images-amazon.com/images/I/51oytt8fP5L._SL1000_.jpg
    [8] => https://images-na.ssl-images-amazon.com/images/I/61Qarg%2BmjrL._SL1000_.jpg
)

I'm trying to figure out how to put $images array to the REST API but still stuck.

Any helps would be much appreciated!

2
  • So you need to format the second array like the first? Commented Aug 21, 2019 at 8:05
  • I'm not sure what would be the best option. Here is the REST API docs for creating new product: woocommerce.github.io/woocommerce-rest-api-docs/… Commented Aug 21, 2019 at 8:08

1 Answer 1

1

You can just create a new array with index images like:

$yourImageArray = array(
    'https://images-na.ssl-images-amazon.com/images/I/61imqvdVv1L._SL1000_.jpg',
    'https://images-na.ssl-images-amazon.com/images/I/61CpVDq9iwL._SL1000_.jpg',
    'https://images-na.ssl-images-amazon.com/images/I/61IMXxbcfpL._SL1000_.jpg',
    'https://images-na.ssl-images-amazon.com/images/I/61HFUejnppL._SL1000_.jpg',
    'https://images-na.ssl-images-amazon.com/images/I/51whKF45l0L._SL1000_.jpg',
    'https://images-na.ssl-images-amazon.com/images/I/51uPvXbo3IL._SL1000_.jpg',
    'https://images-na.ssl-images-amazon.com/images/I/61sNz9zDalL._SL1000_.jpg',
    'https://images-na.ssl-images-amazon.com/images/I/51oytt8fP5L._SL1000_.jpg',
    'https://images-na.ssl-images-amazon.com/images/I/61Qarg%2BmjrL._SL1000_.jpg'
);

$newArray = array();
foreach($yourImageArray as $key => $val){
    $newArray['images'][$key] = array('src'=>$val);
}

echo "<pre>";
print_r($newArray);

DEMO

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks man. I tried this but doesn't work: $data = [ 'name' => $product_name, 'type' => 'simple', 'regular_price' => $product_price, 'sale_price' => $product_sale_price, 'description' => $product_description, 'categories' => [ [ 'id' => $product_category ], ], $newArray ];
@AnthonyL.: that is the new story let me think aboutthis.
print_r($data) check whats wrong in your complete array. @AnthonyL.
I extract the image (the first array level) out, then added as 'images' => $newArray and it worked. Thank you so much
@AnthonyL.: glad to help u

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.