WooCommerce Multilingual is compatible with the WordPress REST API. Using the REST API, you can now create, read, update, and delete translated WooCommerce content. This includes Products, Categories, Orders, and others.

This document is intended for developers who want to leverage these features and offer multilingual WooCommerce experience in their plugins, sites and applications.

Requirements

To use the REST API with WooCommerce Multilingual you need the following:

Setting Up The Client Library

As mentioned, you need the WooCommerce REST PHP client library. This library is a PHP wrapper which allows interaction with the WooCommerce REST API in a more straight-forward way. You can use other client libraries as well.

Setting up the library inside your application involves three steps.

1. Download the library files to your site’s server using the following Composer command:

composer require automattic/woocommerce

 

2. Include the library auto-loading file, autoload.php, at the start of your PHP code using the require function.

WCML REST API- including autoloading file
require __DIR__ . '/vendor/autoload.php';

use Automattic\WooCommerce\Client;

$woocommerce = new Client(

   'http://example.com',

   'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',

   'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',

   [

       'wp_api' => true,

       'version' => 'wc/v1',

   ]

);

3. Generate the Consumer Key and Consumer Secret values.

Go to WooCommerce->Settings page and click the API tab. There, click the Keys/Apps sub-tab and click Add Key. In the form that appears, enter the Description, User and Permissions you want to grant the user and click Generate API Key.

Accessing The Consumer Keys Generation Form On The WooCommerce Settings Page

5. Add the generated Consumer Key and Consumer Secret values to your PHP code.


'ck_61f94473aab0445da506e095e664f5d65b153f45';

'cs_7e60f84b6c87554d249b1ad797d6201566c1009e';

 

6. Configure the options for using the library. You can find the description of all the options on the official page.

Using the REST API

Now that you set up the library in your PHP code, you can start developing different actions for your code to perform. The following list features all main actions you can perform using REST API and WooCommerce Multilingual.

View orders

By adding the “lang” parameter to the request, you can return the contents of orders in specified languages. The only condition is that products are translated into the respective language. One application of this is seeing the orders in the site’s default language.

Example: Get only the orders placed in French.

$orders = $woocommerce->get('orders', array( 'per_page' => 25, 'lang' => 'fr' ) );

 

Related WooCommerce Documentation:

http://woothemes.github.io/woocommerce-rest-api-docs/?php#view-list-of-orders

Get products

By adding the “lang” parameter, you can get products in a specified language. To get all products in all languages the “lang” parameter must be set to “all”.

Example: Get only the Spanish products.

$products = $woocommerce->get('products', array( 'per_page' => 25, 'lang' => 'es' ) );

 

Related WooCommerce Documentation:

https://woocommerce.github.io/woocommerce-rest-api-docs/?php#list-all-products

View Single Product

You can add language and translation information to the returned data, using the following two parameters:

  • Use the lang parameter to show the language of the queried product
  • Use the translations parameter, which is an associative array of product ids for translations in other languages

The output will also include custom prices in secondary currencies if those prices were set and the multi-currency mode is enabled and configured.

Example:

$product = $woocommerce->get('products/99');

 

A result of this example would be similar to the following:

View Single Product
=> stdClass Object
        (
            [title] => An English product
            [id] => 28
            [created_at] => 2015-06-08T08:56:49Z
            [updated_at] => 2015-06-08T08:56:49Z
            [type] => simple
            [status] => publish
            [downloadable] =>
            [virtual] =>
            [permalink] => http://example.com/shop/a-simple-english-product
            [sku] =>
            [price] => 100.00
            [regular_price] => 100.00
            [sale_price] =>
            [price_html] => $100.00
            …
            [variations] => Array
                (
                )
 
            [parent] => Array
                (
                )
 
            [translations] => stdClass Object
                (
                    [bg] => 31
                    [es] => 30
                )
 
           [lang] => en,
            [multi-currency-prices] => Array
     (
         'EUR' => Array
      (
        'regular_price' => 127,
'sale_price' => 100
         ),
         'USD' => Array
      (
        'regular_price' => 156,
'sale_price' => 130
         )
     )
        )

Related WooCommerce Documentation:

https://woocommerce.github.io/woocommerce-rest-api-docs/?php#retrieve-a-product

Get Product Categories and Tags:

By adding the “lang” parameter, you can get product categories and tags in a specified language. To get all products in all languages the “lang” parameter must be set to “all”.

Example: Get categories in all languages.

$categories= $woocommerce->get('products/categories', array( 'per_page' => 10, 'lang' => 'all' ) );

 

Related WooCommerce Documentation:

https://woocommerce.github.io/woocommerce-rest-api-docs/?php#list-all-product-categories

Create products

Three parameters are supported when creating a product:

  • lang – the respective “lang” value will be set for the product. If omitted, the default language of the site will be used.
  • translation_of (optional) – the product will be created as a translation of an existing product, having the id provided by the value of translation_of. If ‘translation_of’ is omitted, the product is created as part of a new translation group (trid)
  • custom_prices – allows settings custom prices in secondary currencies
Create Products
$data = [
    'name' => 'English Test Post',
    'type' => 'simple',
    'regular_price' => '10.00',
    'description' => 'This is a Simple English Test Post',
    'short_description' => 'This is a Simple English Test Post',
    'categories' => [
        [
            'id' => 10
        ],
        [
            'id' => 12
        ]
    ],
    'images' => [
        [
            'src' => 'https://example.com/image-1.png',
            'position' => 0
        ],
        [
            'src' => 'https://example.com/image-2.png',
            'position' => 1
        ]
    ],
    'lang' => 'en',
    'custom_prices' => [
        'EUR' => [
            'regular_price' => 1999,
            'sale_price' => 1500
        ],
        'USD' => [
            'regular_price' => 2100,
            'sale_price' => 2099
        ]
    ]
];

$product = $woocommerce->post('products', $data);

To add a product as a translation of another post, you need to pass the translation_of parameter. If the post that was created with the previous call has an ID of 23, the following would be the code for adding a translation.

Creating a product as translation of another one
$data = [

'name' => 'French Test Post',

'type' => 'simple',

'regular_price' => '10.00',

'description' => 'This is a Simple French Test Post',

'short_description' => 'This is a Simple French Test Post',

'lang' => 'fr',

'translation_of' => '23'

];



$product = $woocommerce->post('products', $data);

Note that if the categories are already translated, you don’t need to specify them explicitly when adding the translation if the Copy taxonomy to translations option is set in WPML. They will be added automatically.

Related WooCommerce Documentation:

http://woocommerce.github.io/woocommerce-rest-api-docs/?php#create-a-product

View Single Order

By providing a “lang” parameter to the request, if the order was placed in a different language, all items, if they exist, will be displayed as translations in the specified language.

Example: See contents of order #110 in English.

$order = $woocommerce->get('orders/110', array( 'lang' => 'en' ) );

 

Related WooCommerce Documentation:

https://woocommerce.github.io/woocommerce-rest-api-docs/?php#retrieve-an-order

Create an Order

Adding a “lang” parameter will set the specified language for the specified order.

Please note that the products, if added in a different language than the one provided as an argument here, will not be converted automatically but added in their language.

Example:

Create an order
$data = [
    'payment_details' => [
        'method_id' => 'bacs',
        'method_title' => 'Direct Bank Transfer',
        'paid' => true
    ],
    'billing_address' => [
        'first_name' => 'John',
        'last_name' => 'Doe',
        'address_1' => '969 Market',
        'address_2' => '',
        'city' => 'San Francisco',
        'state' => 'CA',
        'postcode' => '94103',
        'country' => 'US',
        'email' => 'john.doe@example.com',
        'phone' => '(555) 555-5555'
    ],
    'shipping_address' => [
        'first_name' => 'John',
        'last_name' => 'Doe',
        'address_1' => '969 Market',
        'address_2' => '',
        'city' => 'San Francisco',
        'state' => 'CA',
        'postcode' => '94103',
        'country' => 'US'
    ],
    'customer_id' => 2,
    'line_items' => [
        [
            'product_id' => 58,
            'quantity' => 2
        ],
    ],
    'shipping_lines' => [
        [
            'method_id' => 'flat_rate',
            'method_title' => 'Flat Rate',
            'total' => 10
        ]
    ],
    'lang' => 'el'
    'currency' => 'usd'
];
$order = $woocommerce->post('orders', $data );

Related WooCommerce Documentation:

https://woocommerce.github.io/woocommerce-rest-api-docs/?php#create-an-order