woocommerce / wc-api-python

A Python wrapper for the WooCommerce API.
https://pypi.org/project/WooCommerce/
MIT License
212 stars 112 forks source link

Json format changed #19

Closed marianoduran closed 8 years ago

marianoduran commented 8 years ago

I have been using the API successfully to integrate Categories and Products with existing legacy systems a couple of weeks ago. Developed everything and now it is time to test in the production environment. While in Dvmt environment each time I executed

r = wcapi.get("products/categories?per_page=100") vWooJason = json.loads(r.text)

the Json generated was like (reduced attributed for simplicity) [ { "id":788, "name":"Name1" }, { "id":789, "name":"Name2" }, { "id":790, "name":"NameN" } ]

therefore pagination and len(vWooJason) works fine.

Now without changing anything except installing WP-REST-API plugin on WordPress after executing

r = wcapi.get("products/categories?per_page=100") vWooJason = json.loads(r.text)

the Json generated is

{
"product_categories":[
{ "id":788, "name":"Name1" }, { "id":789, "name":"Name2" }, { "id":790, "name":"NameN" } }

therefore pagination does not work in the same way and len(vWooJason) = 1 instad of the total number of categories.

Questions a) Any idea of why the format of the Json changed? b) Any idea on how to return to the original behaviour?

Regards, Mariano

claudiosanches commented 8 years ago

a) Any idea of why the format of the Json changed? b) Any idea on how to return to the original behaviour?

Not changed, only is a new REST API. You can find more details in https://woocommerce.wordpress.com/2016/04/22/new-rest-api-based-on-the-wp-rest-api-in-2-6/ You just need to set the version that you prefer use.

For WooCommerce legacy versions:

wcapi = API(
    url="http://example.com",
    consumer_key="ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    consumer_secret="cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    version="v3" # or v1 or v2
)

New WooCommerce REST API:

wcapi = API(
    url="http://example.com",
    consumer_key="ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    consumer_secret="cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    wp_api=True,
    version="wc/v1"
)
marianoduran commented 8 years ago

Claudio, thanks for your quick answer.

I started the dvmt back in September 2016 and by that time I was using

wcapi = API( url="http://example.com", consumer_key="ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", consumer_secret="cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", wp_api=True, version="wc/v1" )

and worked properly.

Now it is not working (have tested against 2 WordPress sites). Only worked if I removed that last two parameters

wcapi = API( url="http://example.com", consumer_key="ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", consumer_secret="cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" )

Any idea on how to make the first one work?

Regards, Mariano

claudiosanches commented 8 years ago

Now it is not working (have tested against 2 WordPress sites). Only worked if I removed that last two parameters

Because the new REST API only works on WooCommerce 2.6 as said in the post I liked... Look that this is not a support channel for the REST API.