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.

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.

Read Also:  Send WhatsAPP Message API using PHP Step By Step

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:  Chosen Ajax Autocomplete Textbox using jQuery, PHP

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.