I have set it up in woocommerce.frontity.org as well. It should be working fine now.
I have also set up automatic deploys to Vercel and Changesets in that repo so we can have a changelog of the changes.
I think the proof of concept is good enough at this point to get some external feedback about it.
To be honest, we learned a lot and the Store API shows a lot of promise, so I think we can consider this research a success. Also, I think this opens an opportunity to collaborate more closely with the WooCommerce team
Apart from that, these are my impressions.
Store API
These are the fixes that would make Frontity integration with the Store API easier.
Adding the slug
field to the products
The response of the Store API doesn’t include the slug
yet, which is something we use in Frontity.
For example: https://woocommerce.frontity.org/wp-json/wc/store/products/34
Filtering products by slug
The Store API has a lot of filters, but it doesn’t include a slug
one yet. It would be great to have one as Frontity relies a lot on that to do the match between entities and frontend URLs.
Current filters: https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/trunk/src/StoreApi/docs/products.md#list-products
Renaming permalink
to link
The WordPress REST API uses link
in all their endpoints, but the Store API is using permalink
.
For consistency with the regular REST API, and because Frontity does a transformation of those link
fields to remove the domain so they can be used in decoupled mode, it would be great it the Store API would use link
instead of permalink
.
Remaining work in the package
This is non-exhaustive list of things we still need to do.
Prepare the package for optimistic updates
Right now we are copying everything that we get from the Store API into the state, but we should improve in this regard.
We should prepare the package (state
and actions
) for optimistic updates. That means that:
- The information in the state should be only once. If some information can be derived from other parts of the state, it should be written as derived state. This is the “source of truth” principle.
- Actions should update the state first, then send the request, then revert the state if the request failed.
For example, right now the total price is here:
const total = state.woocommerce.cart.totals.total_price;
But it can be derived from the sum of all the item totals. Something like this:
const total = ({ state }) =>
state.woocommerce.cart.items.reduce(
(total, item) => totel + item.totals.line_total,
0
);
And actually, the total of each item should also be derived from the price of that item, multiplied by the quantity, and so on…
The goal of having a single source of truth is that it simplifies state mutations a lot: if an action modifies a single part of the state, for example the quantity of a product, the rest of the fields will reflect that change immediatly in the UI.
It also means that the application will be less prone to bugs because the derived state is managed by the state manager and nothing can get out of sync.
Handle errors
We should add a way to handle errors, both in the interface and in the state
.
Maybe for the interface this package could show some of the errors using toasts. As toasts are displayed on top of the theme, that would make things easier for theme developers. Of course that would need to be optional.
Support variable products
We should add support for variable products in the theme, to make sure there is no problem with that.
Create the commerce
namespace
We should work on a common commerce
package to allow people to use other implementations, like for example: https://github.com/wp-graphql/wp-graphql-woocommerce.
Make it work with nonces
We should add a way to make this work with nonces. Right now they are disabled.
We could use ajax. Actually, there is something in the WordPress Core already for that, I don’t know if we could use it: https://github.com/WordPress/wordpress-develop/blob/master/src/wp-admin/includes/ajax-actions.php#L5323-L5330
Handlers for the product taxonomies
We should add handlers for the product taxonomies: product categories and tags.
Handlers for the filtered products
We should add handlers for all the queries that WooCommerce supports. Thinkgs like /shop/?orderby=price
and so on. That way, theme creators can add filters that simple use actions.router.set
to render the new list.
Support all themes
For this proof of concept we made a “WooCommerce theme”, but this package should take care of adding the shop, product pages, cart, checkout and thank you pages with a default UI for themes that don’t have direct support for WooCommerce in the same way that you can use WooCommerce with a PHP theme that doesn’t have WooCommerce support.
Support WooCommerce Blocks
We should add support for the new WooCommerce Blocks. Things like capturing the “Add to cart” buttons with a processor and linking those buttons to our actions.woocommerce.addToCart()
actions.
For now it seems like these blocks are not writing HTML to the database and they rely on React for client-side rendering, but I guess that will change at some point. We should ask the WooCommerce team about their plans
Release some ready-to-use components
To facilitate the creation of WooCommerce themes, we could include React components for that can be reused for the common UI elements, like:
- Filters for products: Price range, selectors…
- The Cart.
- The Checkout.
- An Image slider with augmentation tool.
- The Order info.
- And so on.
Product Post Previews
Right now the post previews are not working because the Product
handler is not using the standard REST API and therefore doesn’t have an endpoint to get the last revisions. We should look for a solution for this.
Retrieval of orders by order_key
After a sucessful checkout we are showing the order information because it is stored in the state
, but we don’t have access to it once the users refreshes the page. We need an endpoint where we can fetch the information of an order using the order_key
to simulate what WooCommerce does in WordPress.
Support different payment methods
We should experiment with more realistic payment methods, like Stripe or Paypal.
Last but not least, the main drawback I see with the Store API is that they are not using the standard REST API endpoints for the products and product taxonomies. I am not sure if they studied that option and discarded it. It would be another interesting point to talk with them