ForgeWP

Choose a Cart State Path

Understand the provider and standalone cart contracts before combining components or interpreting local success.

Choose a cart state path

The current source contains two paths. They share a hook name but are not equivalent.

Table · 3 columns
Area
Inside WpCartProvider
Standalone useWpCart
Live transport
WooCommerce wc/store/v1 via fetchStoreApi
Generated forgewp/v1/cart routes
State
CartState with key, product_id and line_subtotal fields
WpCartState with id, productId and lineSubtotal fields
Preview storage
forgewp-cart
forgewp_cart_items and forgewp_cart_state
Drawer controls
Not supplied by the current provider value
isOpen, openCart, closeCart and toggleCart
Clear behavior
Live mode clears provider UI state
Changes local state and attempts a generated clear request

The provider value does not satisfy all members of the declared WpCartContextType; package-wide diagnostics confirm the mismatch. Some ready-made cart components read the older row fields. Do not mix provider components and standalone drawer assumptions without reconciling those contracts.

Standalone actions

The declared hook exposes cart, items, itemCount, subtotal, cartTotal, totals, coupons, currency, drawer controls and isLoading.

  • addItem(product, quantity?, variantLabel?) returns a boolean promise.
  • addToCart(productId, quantity?, variation?) and addToCartBatch(items) return void promises.
  • updateQuantity(lineId, quantity) and removeItem(lineId) return boolean promises.
  • clearCart, removeCoupon, calculateShipping and refreshCart return void promises.
  • applyCoupon(code) returns a boolean promise; setShippingMethod(id) is declared void.

Identify an existing cart row by its line ID/key, not just its product ID.

Server confirmation

Several actions optimistically save local state and resolve despite unsuccessful/null server responses. A changed badge or true result does not always prove persistence. Standalone clearCart currently selects GET for a route registered as POST; standalone removeCoupon calls an unregistered route. Verify the server cart after refresh/reload before promising synchronization.

The provider's preview discount/shipping/tax arithmetic is illustrative. Do not treat it as store policy or use the display total as an authoritative financial calculation.

The drawer recipe uses standalone state. The endpoint reference distinguishes transports and current mismatches.