Title: REST API Categories problem
Last modified: August 30, 2016

---

# REST API Categories problem

 *  Resolved [MattiaRigotti](https://wordpress.org/support/users/mattiarigotti/)
 * (@mattiarigotti)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/rest-api-categories-problem/)
 * Hello,
    I’m trying to import some products through the REST API to my WooCommerce
   site. I’ve already created a category tree within my site. For example suppose
   it’s like this: Shoes->subcategories(Male,Female) T-shirts->subcategories(Male,
   Female)
 * Now when I try to add a product (through the REST API, not the UI) I only can
   associate it to a category by name.
    (e.g. I can write Shoes,Female) But I have
   no certainty that I’m referring to the “Female” subcategory of “Shoes”. In this
   example it would make no sense, but what if I want to refer to the “Female” subcategory
   of “T-shirts”? Is there a way to send something like an ID of the category? I’ve
   tried passing numbers, but it simply creates categories named something like “
   150”, using the id as a name.
 * (I’m using the documentation as a reference [http://woothemes.github.io/woocommerce-rest-api-docs/#products-properties](http://woothemes.github.io/woocommerce-rest-api-docs/#products-properties))
 * Thanks!
 * [https://wordpress.org/plugins/woocommerce/](https://wordpress.org/plugins/woocommerce/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Teowin](https://wordpress.org/support/users/teowin/)
 * (@teowin)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/rest-api-categories-problem/#post-6282309)
 * Hello,
 * I’m not an expert on this. I suppose you use V2 of the API because you can add
   products.
 * I’m writing a management tool with Delphi on Windows which communicates with 
   the WC-API.
 * After quite some trial and error, I learned that the API documentation is wrong
   about the categories: they are added with their ID’s, not their names. And ID
   offcourse takes out the doubt in which category the product is posted.
 * This is my JSON for adding the product, maybe it helps:
 * {
    “product”: { “regular_price”: 33., “categories”: [“27”], “description”: “like
   so”, “short_description”: “Korte omschrijving”, “title”: “Product title” “type”:“
   simple”, “images”: [ { “src”: “http:www.test.nl/457.png”, “alt”: “deze tekst 
   voor het geval er geen afbeelding beschikbaar is.”, “position”: 0 }] } }
 * Best Regards,
    TeoS
 *  Thread Starter [MattiaRigotti](https://wordpress.org/support/users/mattiarigotti/)
 * (@mattiarigotti)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/rest-api-categories-problem/#post-6282316)
 * Thank you for the reply!
    In the end I resolved by modifying wordpress to work
   like it’s advertised on the documentation. If anyone is interested in how to 
   do it, here are some easy steps: – Locate your woocommerce installation folder.
   Should be in “www.your.site/wp-content/plugins” and search for a folder named“
   woocommerce”. – Find class-wc-api-products.php in “woocommerce_folder/includes/
   api. – Open it and go around line 1053. You’ll find something like this:
 *     ```
       // Product categories
       		if ( isset( $data['categories'] ) && is_array( $data['categories'] ) ) {
       			$terms = array_map( 'wc_clean', $data['categories'] );
       			wp_set_object_terms( $product_id, $terms, 'product_cat' );
       		}
       ```
   
 * – You just need to replace “_wc\_clean_” with “_intval_” and the job is done!
 * NOTE: I’m using WooCommerce 2.3.11.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘REST API Categories problem’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [api](https://wordpress.org/support/topic-tag/api/)
 * [categories](https://wordpress.org/support/topic-tag/categories/)
 * [rest](https://wordpress.org/support/topic-tag/rest/)

 * 2 replies
 * 2 participants
 * Last reply from: [MattiaRigotti](https://wordpress.org/support/users/mattiarigotti/)
 * Last activity: [10 years, 11 months ago](https://wordpress.org/support/topic/rest-api-categories-problem/#post-6282316)
 * Status: resolved