Cart and Checkout

9

questions

Can you build a headless store on the Store API?

Yes, that's its main use case together with blocks. But payment on an external frontend depends on the payment gateway: not every gateway supports paying through the Store API.

Does the Store API need authentication?

No, it's the customer's public API. Changes need the Nonce header (CSRF protection) and a session or a Cart-Token.

How do you add a fee to an order?

woocommerce_cart_calculate_fees and $cart->add_fee( $name, $amount, $taxable ). Fees are calculated on the server, so they work in both checkouts.

How do you add a field to block checkout?

With the Additional Checkout Fields API: woocommerce_register_additional_checkout_field() on the woocommerce_init hook, with an id in the namespace/name format, a location and a field type.

How do you add your own data to a Store API response?

With woocommerce_store_api_register_endpoint_data() for the endpoint you need (cart, cart item). The data shows up in the extensions.<namespace> field.

How do you block checkout if the cart has incompatible products?

A check in woocommerce_check_cart_items with an error through wc_add_notice(): a cart with an error won't pass checkout, neither in the template nor through the Store API.

What is the checkout-draft status?

A draft order that block checkout creates while the customer fills in the form. It isn't visible as a regular order and is cleaned up on a schedule if the purchase isn't completed.

Which hooks work in both checkouts?

Everything that happens to the cart and the order on the server: fee and price calculation, coupons, woocommerce_new_order, status changes, woocommerce_payment_complete.

Why doesn't block checkout support the old form hooks?

The form is rendered by React on the client, and the data goes as a JSON request to the Store API. There is no PHP form template, so the hooks that inserted HTML into the form or read $_POST aren't there either.