Answer the question
In order to leave comments, you need to log in
How to do for inside array?
Hello, how can I make the loop work inside the array?
<?php
$arr_tovar = $result->products;
$api_response = wp_remote_post( $url_site.'/wp-json/wc/v3/products', array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'admin:123123123' )
),
/*
'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
)*/
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 );
}
) );
$body = json_decode( $api_response['body'] );
if( wp_remote_retrieve_response_message( $api_response ) === 'Created' ) {
echo 'The product ' . $body->name . ' has been created';
}
print_r( $body );
?>
Answer the question
In order to leave comments, you need to log in
No way. Form function parameters, and then send:
$arr_tovar = $result->products;
$params = [
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'admin:123123123' )
),
'body' => [],
];
foreach ($arr_tovar as $item) {
$params['body'][] = array(
'name' => $item->name,
'status' => 'pending',
'categories' => array(
array(
'id_iiko_категории' => $item->productCategoryId
)
),
'regular_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 'The product ' . $body->name . ' has been created';
}
print_r( $body );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question