WooCommerce comes with a REST API that, for example, allows us to create products. Here I show you how to do this.
Installing and initializing the PHP library for the WooCommerce REST API
There are official libraries for the WooCommerce REST API, which make it possible to use the API – besides via cURL – with Node.js, PHP, Python, and Ruby. Here I show the usage of the PHP library from Automattic. For that, we first need to install it, what is quickly done with Composer (I assume that Composer is already installed for the project):
Code language: Bash (bash)composer require automattic/woocommerce
Now we load it so that we can use the lib:
// Load Composer autoloader.
// @link https://github.com/brightnucleus/jasper-client/blob/master/tests/bootstrap.php#L55-L59
$autoloader = dirname( __FILE__ ) . '/vendor/autoload.php';
if ( is_readable( $autoloader ) ) {
require_once $autoloader;
}
use Automattic\WooCommerce\Client;
$woocommerce = new Client(
'https://shop.local',
'the_consumer_key',
'the_consumer_secret',
[
'wp_api' => true,
'version' => 'wc/v2',
]
);
Code language: PHP (php)
To do that, we include the Composer autoloader if it exists (if the plugin is part of a larger project that is managed via Composer, our subproject would not get an own autoloader file, but the classes would be included in the autoloader of the parent project) and use the use
statement so we can access the Client
class more quickly. Now we create a Client
object and save it in the $woocommerce
variable. These are the parameters we pass to the new Client()
call:
- the shop URL.
- the consumer key.
- the consumer secret.
- an array of additional options, in our case:
- the permission to make requests to the new WP-REST API integration, which is available since WooCommerce 2.6.
- the version of the API.
You can generate consumer secret and consumer key in the WordPress backend under WooCommerce › Settings › API › Keys/Apps.
Creating a product with the REST API
Now let us create a product:
$prod_data = [
'name' => 'A great product',
'type' => 'simple',
'regular_price' => '15.00',
'description' => 'A very meaningful product description',
'images' => [
[
'src' => 'https://shop.local/path/to/image.jpg',
'position' => 0,
],
],
'categories' => [
[
'id' => 1,
],
],
];
$woocommerce->post( 'products', $prod_data );
Code language: PHP (php)
This code adds a product with the title »A great product«, a meaningful description, and a picture, for the price of 15,00 dollar (or whatever is set as the currency of the shop) in the category with the ID 1
.
You can find all available options for products in the docs.
Of course, my example with hard-coded data is a little bit pointless. But in this way, for example, you could loop the products from an export file or string and add them to WooCommerce relatively easy.
i am using Woocommerce’s REST API and in my product list i can add product using API
but what i want is i do not want to display those product which i entered using REST API only Display product which is added within WordPress..
how can i do that ?
Hey,
I am not sure if I understand correctly what you want. You are creating products via the REST API and via the »default« way (so the WordPress backend)? And you want to show only the products that were added via the WordPress backend? Where do you want to show only those products? In the backend products list or in the frontend view?
Best,
Florian
yes you understood correctly...
i am creating product with the REST API and via "default" way...
i only want to display those product that i created via "default" way (usual create product in wordpress) in the frontend view....
product that i created via REST API i Don't want to display those in frontend but in backend it's fine
Okay.
One way to do it should be setting the
status
of the API products toprivate
. That should make them only visible to admins, regarding to the Codex (https://codex.wordpress.org/Post_Status#Private).Another way to hide it from the catalog would be setting the
catalog_visibility
tohidden
.Hope that helps!
Florian
Thank-you so much "catalog_visibility = hidden" worked...
Great to hear that! 🙂
Hey,
im trying to place order from my mobile application but i am not able to figure it out to do so. can you help me with process of authentication and posting the order?
Hi,
if your app would be written in PHP, I maybe could, but I guess it is written in some other language?
Florian
if we have a file that contains several products is its break a problem if we use the api rest to create all these products
Hi Wannis,
sorry, but I don’t understand your comment – do you want to do the import from a file? If so, I cannot help with that, because I have no experience with that…
Best,
Florian
Hi Florian,
Yes I want to import products from a big file that is returned by the stanley stella API.
After I add these products in the base of my site that is developed by WordPress and Woocommerce Plugin
Hi Wannis,
mh, I think I cannot help you with that, I don’t know this stella API, sorry.
Best,
Florian
Hi,
Is there a way to add products and variants without the api?
I've seen people create the api call in php like this:
$data = [
'name' => 'Test product',
'description' => 'Lorem ipsum',
];
$request = new WP_REST_Request( 'POST' );
$request->set_body_params( $data );
$products_controller = new WC_REST_Products_Controller;
$response = $products_controller->create_item( $request );
Why I would like an alternative to your example of rest is you have to actually create the consumer keys, which is a pain for a client if you have a plugin that creates products..
For me personally , I have to check 3 external servers of available products ( plus variants) and update/create products(+ variants) almost every day.. which has to be a plugin that can be shared ..
Hi Miguel,
it is possible to create products via
wp_insert_post()
(https://lukasznowicki.info/insert-new-woocommerce-product-programmatically/). Regarding variable products I found this answer: https://stackoverflow.com/questions/47518280/create-programmatically-a-woocommerce-product-variation-with-new-attribute-valueIn the end of the answer there is a link to another answer that seems to be the first step in this solution.
Did not try anything of that, but maybe it helps?
Best,
Florian
Thank-you for this tutorial. I am getting an unknown error while retrieving customer details. I am also following this tutorial https://www.cloudways.com/blog/custom-dashboard-using-woocommerce-php-rest-api/ but I am stuck and not finding a way to resolve this issue.
Hi Alex,
can you enable error logging on your host? A specific error message should help getting this resolved.
Best,
Florian
Hello i develop android application i face problem in rest api getting json. i get some specific product properties like product id, product name, product sku, product price and last one product image. when i use php lib like print_r($woocommerce->get(products)); then its show all properties which is not require how to solve. ? Thanks
Hi Inam,
I’m sorry, but I don’t understand exactly what you want achieve and where the problem is. Could you try to explain it differently?
Best,
Florian
Hi Florian,
good tutorial. Do you know where the post () method comes from in:
$woocommerce->post('products' , $data). I cannot find any docs on that topic. Thanks.
Hi Chatex,
the method comes from the WooCommerce API wrapper. You can find the code here: https://github.com/woocommerce/wc-api-php
Best,
Florian
Great tutorial