4

I am building a webshop with React, Wordpress v4 and Woocommerce Rest api v2.

I am a bit confused on how to proceed with authentication. I understand that for some requests like place an order or create a new product I need to authenticate.

But I also need to be able to just retrieve all products for a customer who visits the site for the first time and just wants to browse through all our products. What is the best practice here?

My thoughts are:

  • Create a public, separate layer with PHP or Node which securely stores the Woocommerce API keys and provides public endpoints (only GET, i.e. GET /products)
  • Access those endpoints via JS/React and render the products
  • Create another separate layer for placing orders which requires the costumer to authenticate (i.e. POST /orders)

Also, instead of adding a separate layer I could use the Wordpress function add_action( 'rest_api_init',...

Is there a better way to do it or am I missing something? Or can I retrieve the Woocommerce Products somehow via the Wordpress API endpoints without api keys?

Felix Hagspiel
  • 2,634
  • 2
  • 30
  • 43

1 Answers1

6

Okay, somehow I was thinking the wrong way. As wordpress lists all products which were created in woocommerce as a regular Wordpress post with post-type product, I just have to make a request to the Wordpress API (NOT the Woocommerce API!), in my example:

http://laflor.wordpress.local/wp-json/wp/v2/product

Felix Hagspiel
  • 2,634
  • 2
  • 30
  • 43
  • 2
    Interesting, I didn't think of that as well. This is probably the way to go for accessing resources without authentication. Were there any problems you stumbled upon accessing the woocommerce data via the standard wordpress api? – Nico Hauser Dec 12 '17 at 17:10
  • Not for fetching standard data like pages, products, posts. I also added custom wp endpoints as described here https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ for everything there was no endpoint yet (add to cart, remove from cart, etc...). I had some issues, but mostly with woocommerce plugins like flexible shipping, because for me it is hard to understand what happens behind the scenes and most plugins lack developer-documentation. – Felix Hagspiel Dec 13 '17 at 06:51
  • How have you been able to pull the consumer and secret key – Seb Sep 14 '20 at 20:57
  • @Seb I have not tried to get this information, so I do not know. I think you need to provide more information. In addition, I am not sure if you should return a secret key via the API. Maybe you should create a new question and provide more information about what you want to do – Felix Hagspiel Sep 15 '20 at 08:22
  • is there a way to get all categories for products from wordpress rest api? – Vasil Valchev May 26 '22 at 19:21
  • 1
    @VasilValchev yes, you can always add your own endpoints and return whatever you like. https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ – Felix Hagspiel May 27 '22 at 08:01