-1

Is it possible to create new products in woocommerce using its API with javascript from clientside?

I tried this for getting all products:

endpoint:

https://localhost/wpShop/wc-api/v2/products?consumer_key=ck_1111111111122123&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1458225139&oauth_nonce=nVq4rX&consumer_secret=cs_232332322233232&oauth_signature=kzoVx+VYSWlLbRpi3f8222222=

javascript request:

$.ajax({
  method: "GET",
  url: woocommerceURLProduct,
  data: ""
})
  .done(function( msg ) {
    console.log( "products: " );
    console.log( msg );
  });

This works! But I couldn't make this work for creating new products.

same endpoint:

https://localhost/wpShop/wc-api/v2/products?consumer_key=ck_1111111111122123&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1458225139&oauth_nonce=nVq4rX&consumer_secret=cs_232332322233232&oauth_signature=kzoVx+VYSWlLbRpi3f8222222=

my new product:

var data = {
    product: {
      title: 'Premium Quality',
      type: 'simple',
      regular_price: '21.99',
      description: 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
      short_description: 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
      categories: [
        9,
        14
      ],
      images: [
        {
          src: 'http://www.adpic.de/data/picture/detail/Wasserflasche_74756.jpg',
          position: 0
        },
        {
          src: 'http://www.adpic.de/data/picture/detail/Wasserflasche_74756.jpg',
          position: 1
        }
      ]
    }
  };

my ajax request:

$.ajax({
  method: "POST",
  url: woocommerceURLcreateProduct,
  data: $.param(data)
})
  .done(function( msg ) {
    console.log( "Data Saved: " );
    console.log( msg );
  });

I get the error:

{code: "woocommerce_api_missing_callback_param", message: "Missing Parameter data"}

Source:

http://woothemes.github.io/woocommerce-rest-api-docs/v2.html?javascript#create-a-product

  • error sounds descriptive enough, you should inspect what is it that you are actually sendung. Anyway WC is off-topic here – Mark Kaplun Mar 17 '16 at 17:17
0

If you're using SSL (HTTPS URL) you don't need to use oauth1.0 with oauth signature and everything. All you need to do is pass in the consumer_key and consumer_secret in the url

https://localhost/wpShop/wc-api/v2/products?consumer_key=ck_1111111111122123&consumer_secret=cs_232332322233232

That's it! However, if your URL is NOT SSL, then you have to go to the trouble of creating an HMAC-SHA1 keys and all that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.