• Resolved MattiaRigotti

    (@mattiarigotti)


    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 https://woothemes.github.io/woocommerce-rest-api-docs/#products-properties)

    Thanks!

    https://www.ads-software.com/plugins/woocommerce/

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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

    (@mattiarigotti)

    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.