wordpress - Upload multiple images to woo commerce in php

668

I have a site that uses php to upload product information to woo commerce. everything works fine; however i am now stuck trying to work out how to add multiple images. I have got it adding 1 image fine as seen below

$imageURL = "https://www.[website].co.uk/new/wp-content/uploads/products/";
    $imageURL =  $imageURL . $product->ITEMNO . ".JPG"; 

    if (!(false === file_get_contents($imageURL,0,null,0,1))) {
        $data['images'] = [[
            'name' => $product->DESC,
                'src' => $imageURL,
                'alt' => $product->DESC
        ]];
        echo "Image URL: " . $imageURL . "\n";
}

$woocommerce->put('products/'.$searched[0]->id, $data);

If i try adding in another image using$data['image'] = ... with different data; it just overwrites the image

Im assume EITHER theres a different input for adding it to the product gallery instead of just the main product image. but i couldnt see one at https://woocommerce.github.io/woocommerce-rest-api-docs/?php#product-images-properties

OR if its to do with different id's for the image. Of which i tried adding'id' => 56565656, to the$data['images'] but it just crashed on me.

Any help would be appreciated.

768

Answer

Solution:

From woo commerce docs, 'images' is an array type; so it was just figuring out how to write it to get the 2nd image in the second slot of the array; find below the answer

$data['images'] = 
        [
            [
            'name' => $product->DESC,
            'src' => $imageURL,
            'alt' => $product->DESC
        ],
            [
            'name' => $product->DESC2,
            'src' => $imageURL2,
            'alt' => $product->DESC2              
            ]
        ];

People are also looking for solutions to the problem: php - Select mulitple columns in Laravel Request

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.