I am creating a plugin to interact with WooCommerce.
Currently, if I want to create a product I need to generate API credentials in the WooCommerce settings and add those credentials into a form in the menu page created by my plugin which is then, in turn, saving them as wp_options and then accessing them when I connect to the WooCommerce rest API.
These seem like too many steps to follow for anyone else who is using the plugin and might not be too familiar with WordPress.

Is there a way to programmatically generate these credentials or even interact with WooCommerce without these credentials altogether?
This plugin will only run on WordPress and any data in it will remain on the domain it is installed. There will be no external API calls only to the WooCommerce installed on the same WordPress my plugin is installed on.

I managed to create the keys automatically with the WooCommerce auth endpoint but I can't get the keys from the callback url.

  • Is this from PHP or JS? If it's PHP then you can just call the main API directly, e.g. start with WC() to get the WooCommerce object.
    – Rup
    Feb 23, 2022 at 12:17
  • Hi, thank you for responding, it's from a php file in my plugin. do I need to include some other file to get access to the WC() object or is it defined globally?
    – Daniel
    Feb 24, 2022 at 13:19
  • No, as long as the WooCommerce plugin is loaded WC() should be defined globally in WordPress.
    – Rup
    Feb 24, 2022 at 13:52
  • Thank you so much for the info
    – Daniel
    Feb 25, 2022 at 6:52
  • Hi, if you have solved your problem you can write an answer and mark it as accepted so people will be able to see how it was solved. Thanks! Mar 4, 2022 at 11:24

1 Answer 1


Here is my solution.

You can directly interact with WooCommerce if you include the main woocommerce file from woocommerce/woocommerce.php

The classes I specifically used were WC_Product and WC_Product_Query

With these classes, you can get existing products and create new ones. I included links to the docs for these two classes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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