[英]WooCommerce Rest API: Create product with images
我正在嘗試使用WooCommerce .Net Rest API添加產品。 除我的圖像外,其他一切正常。 我不確定如何用PHP添加它們。
我從另一個來源獲得產品,該來源給了我所有用逗號分隔的圖像。 當我檢索它們時,我會像這樣制作一個數組:
$afbeeldingen = explode(",", $_POST['afbeeldingen']) ;
在接下來的部分中,我迷路了。我必須將圖像URL放入“圖像”數組中,如下所示。 我當時正在考慮使用一些foreach循環,但不確定如何使它工作。
$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
]
]
];
我將不勝感激!
您需要遍歷afbeeldingen
每個元素,並使用鍵src
為每個元素創建一個數組,然后將其推入存儲所有src元素的另一個數組。 下面的代碼應該做到這一點。
$images = array();
$size = count($afbeeldingen);
for ($i=0; $i < $size; $i++) {
$img = array("src" => $afbeeldingen[$i]);
array_push($images, $img);
}
這是另一種方法。 選擇他們從事相同工作的人。
$images = array();
foreach ($afbeeldingen as $img) {
array_push($images, array("src" => $img));
}
使用它來創建圖像數組。 然后將prod_data
中的images值設置為創建的images數組。
'images' => $images
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.