Creating a WooCommerce product with PHP via the REST API

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):

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',
	]
);

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 WooCommerceSettingsAPIKeys/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 );

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.

Related posts

8 comments on »Creating a WooCommerce product with PHP via the REST API«

  1. krishna

    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 ?

    Reply
    1. Florian Brinkmann

      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

      Reply
      1. krishna

        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

        Reply
  2. Junaid Akram

    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?

    Reply

Leave a Comment

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