How to Add JSON Data into WooCommerce WordPress Database from API

Add products from REST API to WooCommerce, update WordPress database WooCommerce tables with php code

Here I have used github countries api to test. In this php code countries name and code is inserted directly into WooCommerce products  into WordPress database.

<?php /* Template Name: awsapi */

 

$sl =file_get_contents(‘https://gist.github.com/keeguon/2310008 ‘);
$slices=json_decode($sl);
foreach ($slices as $slice) {
$title = $slice->name;
$content = $slice->code;
// insert more logic here
//echo $content;
// Create post object
$data = [
‘name’ => $title,
‘description’ => $content,

];
$request = new WP_REST_Request( ‘POST’ );
$request->set_body_params( $data );
$products_controller = new WC_REST_Products_Controller;
$response = $products_controller->create_item( $request );
}

?>

Leave a Reply