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 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:
- WordPress 4.7+
- WooCommerce 3.9.0+
- WooCommerce Multilingual 4.11.0+
- WooCommerce REST PHP client library
- Pretty permalinks set on the Settings → Permalinks page.
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 straightforward way. You can use other client libraries as well.
Setting up the library inside your application involves a few steps:
- Download the library files to your site’s server using the following Composer command:
- Include the library auto-loading file, autoload.php, at the start of your PHP code using the require function.
- Generate the Consumer Key and Consumer Secret values. To do this, go to WooCommerce → Settings 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.
- Add the generated Consumer Key and Consumer Secret values to your PHP code.
- Configure the options for using the library. You can find the description of all the options on the official page.
composer require automattic/woocommerce
'ck_61f94473aab0445da506e095e664f5d65b153f45'; 'cs_7e60f84b6c87554d249b1ad797d6201566c1009e';
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: List all orders
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: Retrieve an order
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: List all products
View Single Product
The returned data will include language and translation information. Additionally, if custom prices in secondary currencies are set and the multi-currency mode is both enabled and configured, those prices will be included as well.
Example:
$product = $woocommerce->get('products/99');
A result of this example would be similar to the following:
=> 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 )
Related WooCommerce Documentation: Retrieve a product
Get Product Taxonomy Terms (Product Categories, Tags, Attributes, Shipping Classes)
By adding the lang parameter, you can get product taxonomy term in a specified language. To get all terms 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: List all product categories
Sales Reports
By adding the currency parameter, you can get reports in a specified currency.
$query = [ 'period' => 'year', 'currency' => 'EUR' ]; $woocommerce->get( 'reports/sales', $query );
Related WooCommerce Documentation: Retrieve sales report
Top Sellers Reports
By adding the lang parameter, you can get reports in a specified language.
$query = [ 'lang' => 'fr' ]; $woocommerce->get( 'reports/top_sellers', $query );
Related WooCommerce Documentation: Retrieve top sellers report
Products Totals Reports
By adding the lang parameter, you can get reports in a specified language.
$query = [ 'lang' => 'fr' ]; $woocommerce->get( 'reports/products/totals', $query );
Related WooCommerce Documentation: Retrieve products totals
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.
Example:
$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:
$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: Create a product
Variable Products
Create product variations with local attribute
Three parameters are supported when creating a product variation:
- lang – the respective lang value will be set for the product variation. If omitted, the default language of the site will be used.
- translation_of (optional) – the product variation will be created as a translation of an existing product variation, having the ID provided by the value of translation_of. If translation_of is omitted, the product variation is created as part of a new translation group (trid).
- custom_prices – allows settings custom prices in secondary currencies.
Example:
$data = [ 'lang' => 'en', 'regular_price' => '10', 'attributes' => [ [ 'name' => $local_attribute_name, 'option' => 'red' ] ] ]; $woocommerce->post( 'products/{$variable_product_id}/variations', $data );
To add a product variation 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 25, the following would be the code for adding a translation:
$data = [ 'name' => 'Translation of Variable Product', 'type' => 'variable', 'description' => 'Translate of Product Description', 'lang' => 'fr', 'translation_of' => 25, 'attributes' => [ [ 'name' => $local_attribute_name, 'visible' => true, 'variation' => true, 'options' => [ '$translation_of_red' ] ] ] ]; $woocommerce->post('products', $data);
Related WooCommerce Documentation: Create a product variation
Create Product Variations with Global Attribute
First, you need to create and translate the global terms. Then, you can create the variable product by following the example below:
$data = [ 'name' => 'Test Variable Product', 'type' => 'variable', 'description' => 'Test Variable Product for REST', 'lang' => 'en', 'translation_of' => null, 'attributes' => [ [ 'id' => $global_attribute_id, 'visible' => true, 'variation' => true, 'options' => [ 'Red' ] ] ] ]; $woocommerce->post('products', $data);
Then we need to create product variations. If the original product has an ID of 25, then the following would be an example of the code:
$data = [ 'create' => [ [ 'lang' => 'en', 'translation_of' => null, 'regular_price' => '10', 'attributes' => [ [ 'id' => $global_attribute_id, 'option' => 'blue-en' //we need to add here the attribute term slug ] ] ] $woocommerce->post( 'products/25/variations/batch', $data );
Now the variable products have been created in English. To translate it, follow the example below:
$data = [ 'name' => 'FR Test Variable Product', 'type' => 'variable', 'description' => 'FR Test Variable Product for REST', 'lang' => 'fr', 'translation_of' => 25, 'attributes' => [ [ 'id' => $global_attribute_id, 'visible' => true, 'variation' => true, 'options' => [ 'red-fr' //slug of the attribute term translation ] ] ] ]; $woocommerce->post('products', $data);
Related WooCommerce Documentation: Batch update product variations
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 instead be added in their language.
Example:
$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: Create an order
Creating Taxonomy Terms (Product Category, Tag, Attribute, Shipping Class)
Two parameters are supported when creating a term:
- lang – the respective lang value will be set for the term. If omitted, the default language of the site will be used.
- translation_of (optional) – the term will be created as a translation of an existing term, having the id provided by the value of translation_of. If translation_of is omitted, the term is created as part of a new translation group (trid)
Example: Create a category in the second language.
$category = [ 'name' => 'Product Category ES', 'translation_of' => $original_category_id, 'lang' => 'es' ]; $woocommerce->post( 'products/categories', $product_category );
Related WooCommerce Documentation: Create a product category
Example: Translate a global attribute term in the secondary language
$attribute_term = [ 'name' => 'Black ES', 'translation_of' => $original_attribute_term_id, 'lang' => 'es' ]; $woocommerce->post( 'products/attributes/{$attribute_id}/terms', $attribute_term );
Related WooCommerce Documentation: Create an attribute term