0

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.

5
  • 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. Commented 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? Commented Feb 24, 2022 at 13:19
  • No, as long as the WooCommerce plugin is loaded WC() should be defined globally in WordPress. Commented Feb 24, 2022 at 13:52
  • Thank you so much for the info Commented 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! Commented Mar 4, 2022 at 11:24

1 Answer 1

0

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.