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
Code language: Bash (bash)

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 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 );
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.

21 reactions on »Creating a WooCommerce product with PHP via the REST API«

  1. 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 ?

    1. 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

      1. 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

  2. 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?

  3. 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

      1. 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

  4. 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 ..

    1. 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-value
      In 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

  5. 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

  6. 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.

Leave a Reply

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