Почему не получается создать товары из масcива?
Код:
<?php
// $max_tover = count($result->products);
// for ($i = 1; $i <= count($result->products); $i++)
// {
// $hi_products[$i] = array(
// 'name' => $arr_tovar[$i]->name,
// 'status' => 'pending',
// 'categories' => array(
// array(
// 'id_iiko_категории' => $arr_tovar[$i]->productCategoryId
// )
// ),
// 'regular_price' => $arr_tovar[$i]->price );
// }
$arr_tovar = $result->products;
$params = [
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'admin:123123123' )
),
'body' => [],
];
/*
'body' => array(
'name' => 'My test2',
'status' => 'pending',
'categories' => array(
array(
'id' => 15
)
),
'regular_price' => '9.99'
// more params http://woocommerce.github.io/woocommerce-rest-api-docs/?shell#product-properties
)*/
foreach ($arr_tovar as $item) {
$params['body'][] = array(
'name' => $item->name,
'status' => 'pending',
'categories' => array(
array(
'id_iiko_категории' => $item->productCategoryId
)
),
'price' => $item->price
);
}
$api_response = wp_remote_post( $url_site.'/wp-json/wc/v3/products', $params);
$body = json_decode( $api_response['body'] );
if( wp_remote_retrieve_response_message( $api_response ) === 'Created' ) {
echo 'Этот продукт ' . $body->name . ' успешно создан';
}else{
echo 'Ошибка'. wp_remote_retrieve_response_message( $api_response );
}
echo '<pre> $body: <br>';
var_dump( $body );
echo '</pre><br><br>';
echo '<pre> $params["body"]<br>';
var_dump( $params['body'] );
echo '</pre>';
?>
var_dump( $body ); выводит:
object(stdClass)#17280 (66) {
["id"]=>
int(277)
["name"]=>
string(10) "Товар"
["slug"]=>
string(8) "tovar-20"
["permalink"]=>
string(40) "http://localhost/ecler/product/tovar-20/"
["date_created"]=>
string(19) "2020-05-15T13:38:32"
["date_created_gmt"]=>
string(19) "2020-05-15T10:38:32"
["date_modified"]=>
string(19) "2020-05-15T13:38:32"
["date_modified_gmt"]=>
string(19) "2020-05-15T10:38:32"
["type"]=>
string(6) "simple"
["status"]=>
string(7) "publish"
["featured"]=>
bool(false)
["catalog_visibility"]=>
string(7) "visible"
["description"]=>
string(0) ""
["short_description"]=>
string(0) ""
["sku"]=>
string(0) ""
["price"]=>
string(0) ""
["regular_price"]=>
string(0) ""
["sale_price"]=>
string(0) ""
["date_on_sale_from"]=>
NULL
["date_on_sale_from_gmt"]=>
NULL
["date_on_sale_to"]=>
NULL
["date_on_sale_to_gmt"]=>
NULL
["price_html"]=>
string(0) ""
["on_sale"]=>
bool(false)
["purchasable"]=>
bool(false)
["total_sales"]=>
int(0)
["virtual"]=>
bool(false)
["downloadable"]=>
bool(false)
["downloads"]=>
array(0) {
}
["download_limit"]=>
int(-1)
["download_expiry"]=>
int(-1)
["external_url"]=>
string(0) ""
["button_text"]=>
string(0) ""
["tax_status"]=>
string(7) "taxable"
["tax_class"]=>
string(0) ""
["manage_stock"]=>
bool(false)
["stock_quantity"]=>
NULL
["stock_status"]=>
string(7) "instock"
["backorders"]=>
string(2) "no"
["backorders_allowed"]=>
bool(false)
["backordered"]=>
bool(false)
["sold_individually"]=>
bool(false)
["weight"]=>
string(0) ""
["dimensions"]=>
object(stdClass)#17279 (3) {
["length"]=>
string(0) ""
["width"]=>
string(0) ""
["height"]=>
string(0) ""
}
["shipping_required"]=>
bool(true)
["shipping_taxable"]=>
bool(true)
["shipping_class"]=>
string(0) ""
["shipping_class_id"]=>
int(0)
["reviews_allowed"]=>
bool(true)
["average_rating"]=>
string(1) "0"
["rating_count"]=>
int(0)
["related_ids"]=>
array(5) {
[0]=>
int(266)
[1]=>
int(267)
[2]=>
int(276)
[3]=>
int(259)
[4]=>
int(271)
}
["upsell_ids"]=>
array(0) {
}
["cross_sell_ids"]=>
array(0) {
}
["parent_id"]=>
int(0)
["purchase_note"]=>
string(0) ""
["categories"]=>
array(1) {
[0]=>
object(stdClass)#17294 (3) {
["id"]=>
int(17)
["name"]=>
string(35) "основная категория"
["slug"]=>
string(6) "eklery"
}
}
["tags"]=>
array(0) {
}
["images"]=>
array(0) {
}
["attributes"]=>
array(0) {
}
["default_attributes"]=>
array(0) {
}
["variations"]=>
array(0) {
}
["grouped_products"]=>
array(0) {
}
["menu_order"]=>
int(0)
["meta_data"]=>
array(0) {
}
["_links"]=>
object(stdClass)#17297 (2) {
["self"]=>
array(1) {
[0]=>
object(stdClass)#17285 (1) {
["href"]=>
string(49) "http://localhost/ecler/wp-json/wc/v3/products/277"
}
}
["collection"]=>
array(1) {
[0]=>
object(stdClass)#17298 (1) {
["href"]=>
string(45) "http://localhost/ecler/wp-json/wc/v3/products"
}
}
}
}
var_dump( $params['body'] ); выводит >400 товаров. Вот 2 из них:
array(462) {
[0]=>
array(4) {
["name"]=>
string(56) "Коробка Vip именная на 10 эклеров"
["status"]=>
string(7) "pending"
["categories"]=>
array(1) {
[0]=>
array(1) {
["id_iiko_категории"]=>
NULL
}
}
["price"]=>
int(1300)
}
[1]=>
array(4) {
["name"]=>
string(53) "Коробка VIP именная на 5 элеров"
["status"]=>
string(7) "pending"
["categories"]=>
array(1) {
[0]=>
array(1) {
["id_iiko_категории"]=>
NULL
}
}
["price"]=>
int(1000)
}