AlexioHill
AlexioHill

Reputation: 49

Upload multiple images to woo commerce in php

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.

Upvotes: 0

Views: 135

Answers (1)

AlexioHill
AlexioHill

Reputation: 49

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

Upvotes: 0

Related Questions