WooCommerce REST API CRUD Tutorial From Scratch

Today, We want to share with you WooCommerce REST API CRUD Tutorial From Scratch.In this post we will show you wordpress woocommerce rest api tutorial, hear for WooCommerce REST API – Create, Update or Remove Products we will give you demo and example for implement.In this post, we will learn about Creating a WooCommerce product with PHP via the REST API with an example.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

WooCommerce REST API CRUD Tutorial From Scratch

There are the Following The simple About WooCommerce REST API CRUD Tutorial From Scratch Full Information With Example and source code.

As I will cover this Post with live Working example to develop Create, update, delete products with WooCommerce REST API, so the wordpress woocommerce rest api endpoints for this example is following below.

WooCommerce Create a Product

$woocommerce_api_results = wp_remote_post( ‘https://domain-name.com/wp-json/wc/v2/products’, array(
‘headers’ => array(
‘Authorization’ => ‘Basic ‘ . base64_encode( ‘KEY:SECRET’ )
),
‘body’ => array(
‘name’ => ‘Legion Y7000 Laptop’, // product title
‘status’ => ‘draft’, // product status, default: publish
‘categories’ => array(
array(
‘id’ => 85 // each category in a separate array
),
array(
‘id’ => 98
)
),
‘regular_price’ => ‘74990.98’ // product price
// PHP Call more params http://woocommerce.github.io/woocommerce-rest-api-docs/?shell#product-properties
)
) );

$data = json_decode( $woocommerce_api_results[‘body’] );
//print_r( $data );

if( wp_remote_retrieve_response_message( $woocommerce_api_results ) === ‘Created’ ) {
echo ‘The product ‘ . $data->name . ‘ has been created’;
}

Update Product Price with WooCommerce REST API

$woocommerce_api_results = wp_remote_post( ‘https://domain-name.com/wp-json/wc/v2/products/{PRODUCT ID}’, array(
//’method’ => ‘PUT’,
‘headers’ => array(
‘Authorization’ => ‘Basic ‘ . base64_encode( ‘KEY:SECRET’ )
),
‘body’ => array(
‘regular_price’ => ‘89582.30’, // simple update the product price
// but we can update data several products parameters at the same time
// more params http://woocommerce.github.io/woocommerce-rest-api-docs/?shell#product-properties
)
) );

$data = json_decode( $woocommerce_api_results[‘body’] );
//print_r( $data );

if( wp_remote_retrieve_response_message( $woocommerce_api_results ) === ‘OK’ ) {
echo ‘The product ‘ . $data->name . ‘ has been updated’;
}

WooCommerce Remove a Product

$woocommerce_api_results = wp_remote_post( ‘https://domain-name.com/wp-json/wc/v2/products/{PRODUCT ID}’, array( // ?force=true
‘method’ => ‘DELETE’,
‘headers’ => array(
‘Authorization’ => ‘Basic ‘ . base64_encode( ‘KEY:SECRET’ )
)
) );

$data = json_decode( $woocommerce_api_results[‘body’] );
//print_r( $data );

if( wp_remote_retrieve_response_message( $woocommerce_api_results ) === ‘OK’ ) {
echo ‘The product ‘ . $data->name . ‘ has been removed’;
}

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about WooCommerce REST API CRUD Tutorial From Scratch.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   laravel Add cascade on delete (ON DELETE CASCADE)
  2.   Manage/Create Laravel 5/6/7 Widgets Using Blade files
  3.   Simple Laravel 5 Vue JS Ajax CRUD(insert update delete)
  4.   select query in php mysql with example
  5.   JQuery Set Focus on First TextBox
  6.   Angular 6 Hello World Application from Scratch
  7.   Laravel Eloquent Update increment decrement Database column value
  8.   Automatically Timeout-Logout Destroy Session inactivity Using PHP
  9.   Remove null from array Jquery Example
  10.   Angular 6 CRUD Tutorial Example From Scratch

Leave a Reply

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