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.

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.

Read Also:  Codeigniter delete query from Database

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 :

Read Also:  Laravel 6 Inserting & Updating Eloquent Models

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. Read Also:  Bootstrap 5 star rating Example
  2. Read Also:  Vue router Get Current URL in Vue.js Example
  3. Read Also:  Laravel 6 Update Increment column
  4. Read Also:  AngularJS Fancybox Popup Simple Example
  5. Read Also:  Angular 6 applications - Insert, Fetch , Edit - update , Delete Operations
  6. Read Also:  Print last query in laravel eloquent - Examples
  7. Read Also:  Laravel 6 Ajax post data Example
  8. Read Also:  Http Post Method using Angular Example
  9. Read Also:  Angular 6 CRUD Tutorial Example From Scratch
  10. Read Also:  Vue.js CRUD Example Tutorial For Beginners