WooCommerce announced at this time that its new Retailer API is now steady after two years in improvement. It powers the plugin’s new block-based cart and checkout expertise in addition to all merchandise blocks.
The Retailer API is a public, unauthenticated API, that gives REST API endpoints for cart, checkout, and product performance. WooCommerce core developer Mike Jolley defined how the brand new API is completely different from the plugin’s REST API:
The principle distinction between the Retailer API and WC REST API is that the Retailer API permits unauthenticated entry to retailer knowledge, for instance, merchandise. This makes it appropriate for quite a lot of use-cases, together with customized frontends.
That is an unauthenticated API. It doesn’t require API keys or authentication tokens for entry.
One other distinction is that the Retailer API has assist for Cart knowledge. This enables for cart operations akin to including objects to the cart, updating portions, making use of coupons, and submitting orders. Solely the present buyer’s cart may be accessed.
Information returned from the Retailer API is at all times reflective of the present consumer (cookie-based), whereas the WC REST API permits extra in depth entry to any knowledge, ought to you will have the proper entry rights.
In an interview with HollerWP final yr, Jolley mentioned it’s useful to consider the Retailer API as for shoppers and the core REST API because the API for retailer administration.
“Preserving them separate like this makes it clear that one exposes knowledge in a public context,” he mentioned.
Scott Bolinger, co-founder of AppPresser, defined how the Retailer API fixes an issue with the unique WooCommerce REST API.
“It [the WC REST API] was by no means meant for cellular, headless, or some other entrance finish implementation,” Bolinger mentioned. “It was solely meant for again finish admin stuff like stock, including/eradicating merchandise, and many others.
“The issue is that there was no various, so headless/cellular apps used it for displaying merchandise. With the WC REST API, every part requires authentication, which doesn’t make sense, as a result of merchandise are public in your web site. You shouldn’t need to authenticate to see somebody’s merchandise.”
The brand new Retailer API permits builders to get merchandise, attributes, collections, and different knowledge with out authentication, and provides a cart API, making it attainable to checkout.
“I feel most implementations of the WC Relaxation API which are for the entrance finish ought to swap to the Retailer API,” Bolinger mentioned.
The WooCommerce staff remains to be engaged on making this API appropriate for headless and cellular apps. In a super world, the very best cart API for headless WooCommerce apps can be made by WooCommerce. Within the meantime, Bolinger advisable CoCart.
CoCart is a customizable, WordPress REST API for WooCommerce that permits builders to construct headless (or decoupled) e-commerce shops utilizing their selection of frameworks.
“I’m not utilizing the Retailer API and I don’t see that I’ll,” Co-Cart founder Sébastien Dumont mentioned. “Each API’s are distinctive for his or her particular person functions. WooCommerce’s Retailer API is designed for the Gutenberg blocks, which solely requires a set format and remains to be inclined for use on native storefronts.
“I don’t see at this time’s information affecting CoCart. Most of my customers have already identified in regards to the Retailer API whereas it was nonetheless experiential and failed after attempting it for headless functions. It additionally requires tweaking whereas CoCart doesn’t”.
Dumont mentioned the Retailer API remains to be lacking loads of invaluable info that builders require and that utilizing CoCart saves them appreciable improvement time.
“With the WC Relaxation API, there is no such thing as a cart or checkout, so it’s cool they added that,” Bolinger mentioned. “Nonces solely work when you find yourself on the location although, so for cellular apps or headless WP, you’ll run into points. Sadly it’s not the place it must be for headless (but).
“There’s a filter to disable nonce authentication:
add_filter( 'woocommerce_store_api_disable_nonce_check', '__return_true' ); Even altering this to make use of one thing like JWT gained’t totally work for headless, not less than in my testing. I hope they are going to handle this sooner or later.”
This explicit issue remains to be open on the WooCommerce GitHub repository. Maintainers appear open to exploring how core can accommodate this. Within the meantime, third-party options can be found to builders constructing cellular apps.
For a technical breakdown of what’s at present attainable with the brand new Retailer API, take a look at Mike Jolley’s announcement publish. He demonstrated how one can undergo the complete buy circulation utilizing the Insomnia API client- with out even visiting the shop. The API’s technical documentation and guide to extensibility is offered on GitHub.