Home / Wordpress / How to Add JSON Data into WooCommerce WordPress Database from API

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 );
}

?>

About Abid

I am Abid Hussain a professional software developer, worked on dozens of projects specializing in web development. I also provide web development service on Fiverr and Freelancer.

Leave a Reply

Your email address will not be published. Required fields are marked *