V
V
Vladimir Kulikov2020-05-15 11:49:01
PHP
Vladimir Kulikov, 2020-05-15 11:49:01

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

1 answer(s)
I
Ilya, 2020-05-15
@it_proger29

No way. Form function parameters, and then send:

the code
$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 );

And I advise you to read about codestyle: https://www.php-fig.org/psr/psr-1/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question