0
votes

I'm trying to add products using the WooCommerce .Net Rest API. Everything is working fine except for my images. I'm not sure how to add them with PHP.

I get my products from another source that gives me all the images separated by a comma. When I retrieve them I make an array out of it like this:

    $afbeeldingen = explode(",", $_POST['afbeeldingen']) ;

For the following part I'm lost.. I have to put the images urls inside the 'images' array like the way it is shown below. I was thinking about using some foreach loop but I'm not sure how to make this work.

$prod_data = [
    'name'              => $_POST['merk'] . ' ' . $_POST['model'] . ' ' . $_POST['type'],
    'type'              => 'simple',
    'sku'               => $_POST['voertuignr_hexon'],
    'regular_price'     => $_POST['verkoopprijs_particulier'],
    'description'       => $_POST['opmerkingen'],
    'images'            => [
        [
            'src' => image1
        ],
        [
            'src' => image2
        ]
    ]
];

I would appreciate any help that I can get with this!

1

1 Answers

0
votes

You need to iterate through each element of afbeeldingen and create an array for each with the key src then push them to another array that stores all the src elements. Below code should do that.

$images = array();
$size = count($afbeeldingen);
for ($i=0; $i < $size; $i++) { 
    $img = array("src" => $afbeeldingen[$i]);
    array_push($images, $img);
}

Here is an alternative way. Choose which ever they do the same job.

$images = array();
foreach ($afbeeldingen as $img) {
    array_push($images, array("src" => $img));
}

Use this to create your images array. Then set the value of images in prod_data to the images array created.

'images'        =>  $images