Код импортирует один продукт, когда я устанавливаю переменную для одного элемента в массиве, как и ожидалось. Когда я снимаю $ product_name [0] и меняю его на $ product_name, чтобы импортировать весь массив продуктов, я получаю следующие ошибки:
function get_malab_api_and_inport()
{
global $feed;
$data = json_decode($feed, TRUE);
$woocommerce = getWoocommerceConfig();
$newProducts = array();
$product_name = array_column($data['results'], 'product_name');
$manufacturer = array_column($data['results'], 'manufacturer');
$image_path = array_column($data['results'], 'image_path');
$product_spec = array_column($data['results'], 'product_spec');
$price= array_map(function($num){return number_format($num,2);}, $cost);
$newProduct = [
'name' => $product_name,
'type' => 'simple',
'regular_price' => $price,
'description' => $product_spec,
'short_description' => $manufacturer,
'categories' => [
[
'id' => 15
]
],
'images' => [
[
'src' => $image_path
]
]
];
array_push($newProducts, $newProduct);
foreach ($newProducts as $nproduct) {
try {
$woocommerce->post('products', $nproduct);
} catch (HttpClientException $e) {
\print_r($e);
\print_r($e->getMessage() . PHP_EOL);
\print_r('Code: ' . $e->getResponse()->getCode() . PHP_EOL);
\print_r('Body: ' . $e->getResponse()->getBody() . PHP_EOL);
}
}
}
Неустранимая ошибка: Uncaught Automattic \ WooCommerce \ HttpClient \ HttpClientException: Ошибка: неверные параметры: имя, описание, short_description, normal_price [rest_invalid_param] в / home / xxxx / xxxx / wp-content / themes / xxxx / functions /MaLab-Api/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.
php: 350 Трассировка стека: # 0 /home/xxxx/xxxx/wp-content/themes/xxxx/functions/MaLab-Api/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.
php (386): Automattic \ WooCommerce \ HttpClient \ HttpClient-> lookForErrors (Object (stdClass)) # 1 / home / xxxx / xxxx / wp-content / themes / xxxx / functions / MaLab-Api / vendor / automattic /woocommerce/src/WooCommerce/HttpClient/HttpClient.
php (422): Automattic \ WooCommerce \ HttpClient \ HttpClient-> processResponse () # 2 / home / xxxx / xxxx / wp-content / themes / xxxx / functions / MaLab-Api / vendor / automattic / woocommerce / src /WooCommerce/Client.
php (56): Automattic \ WooCommerce \ HttpClient \ HttpClient-> request ('products', в / home / xxxx / xxxx / wp-content / themes / xxxx / functions / MaLab-Api / vendor / automattic / woocommerce /src/WooCommerce/HttpClient/HttpClient.php в строке 350