We built DEV3 AI Shared Stock & Inventory as a WooCommerce plugin in PHP with a React admin. A stock pool holds the real units; products, variations and attribute values link to it with a number of units per sale.
Key design decisions
- Each order line remembers what it took. On first deduction a line stores its pool map (pool → units per item); later syncs use that map, not today's links, so a cancellation returns exactly what was taken.
- Target-based, idempotent sync. Every hook computes the line's target ((quantity − restocked) × units, or 0 when the order doesn't hold stock) and moves each pool by the difference from what is already deducted. Repeated or out-of-order WooCommerce hooks can't double-count.
- Atomic pool updates. Pool stock changes with a single
UPDATE … SET stock = stock + delta, never read-modify-write in PHP, and every change is written to a history log with its order, item and reason.
- Link resolution with precedence. Every matching link applies, so a party box linked to cans and cups is a kit. When several links point at the same pool, a variation link beats a product link, which beats an attribute rule.
- A whole-cart check. WooCommerce checks stock per product, so a 6-pack and singles could each pass while together they need more cans than exist. A cart guard sums demand per pool across the classic cart, the block cart and the Store API checkout.
- Product stock mirrored. Each linked product's own WooCommerce stock is set to pool stock ÷ units per sale (the lowest across its pools), so stock text, the quantity box, feeds and the REST API all show the shared number.
Architecture
Pools, links, purchase orders, alerts and the stock log live in custom tables. Forecasts are statistics first (a blended daily rate, trend, weekday pattern and a 95% safety buffer), giving days left and a reorder quantity and date. With the Google Gemini API connected, DEV3 AI explains forecasts in plain words, drafts purchase-order emails, proposes pools from the catalogue, explains anomalies such as an extra zero typed by hand, and answers questions about the inventory. Every AI result is a draft, and every feature has a non-AI fallback. Product Bundles, Composite Products and Mix and Match lines can deduct as the container, the children or both. The API client, encrypted key storage and usage logs come from a shared core used across DEV3's plugins.