Three rules govern the plugin. The first two are in both editions; the third belongs to the full edition's aging report.
Rule 1: the cost is the one you already typed
The cost lives in product meta _gmcg_cost — the key Gatemi Profit Reports and Gatemi Cost of Goods use too — and this plugin both reads and writes it. A second box asking for the same number would be two numbers that disagree, and a merchant finds out which one was used from a report.
And a product with no cost is unknown, never nought. Counting a missing cost as zero makes the stock look cheaper than it is and the margin look better than it is — two errors that point the same flattering way, and neither looks wrong. So:
- Uncosted products are counted apart: how many, how many units, what they are worth at retail.
- They are left out of Value at cost.
- The Margin is measured only over the retail value of stock that actually has a cost behind it.
Rule 2: only stock the store manages is stock
WooCommerce's Manage stock switch is the difference between a quantity that means something and no quantity at all. A product that does not manage stock is not valued at nought — it is left out.
That also keeps the report fast. It asks WooCommerce only for published products that manage stock, so a shop with forty thousand products and four hundred counted ones reads four hundred.
A negative quantity is real. It is valued negatively, because a backorder is stock the shop owes, and clamping it to zero would report a fuller shelf than there is.
Building the valuation, step by step
- Ask WooCommerce for published products and variations with Manage stock on, 200 at a time (the
gmsvf_batch/gmsv_batchfilter), ordered by id. - For each product with a stock number:
- add its quantity to Units on the shelf;
- count it as Out of stock if the quantity is 0 or less, and On backorder if it is below 0;
- find its cost: the variation's own, else its parent's;
- value it at retail: current price × quantity;
- if it has a cost, value it at cost: cost × quantity; otherwise count it as uncosted.
- Stop at 20,000 products (the
…_ceilingfilter). A line on the screen says so if the shop has more. - Work out the margin over the costed stock only.
- Sort the rows by retail value, dearest first, and cut the table to the list length. The totals are never cut.
Nothing is held in memory beyond the rows: products are read a page at a time.
A worked example
| Product | Qty | Price | Cost | At cost | At retail |
|---|---|---|---|---|---|
| Kettle | 10 | £30 | £12 | £120 | £300 |
| Teapot | 5 | £20 | Not set | — | £100 |
| Mug | −2 | £8 | £3 | −£6 | −£16 |
- Value at retail: £300 + £100 − £16 = £384
- Value at cost: £120 − £6 = £114 (the teapot is not counted as free)
- Costed retail: £384 − £100 = £284
- Margin held in stock: £284 − £114 = £170, 59.86%
- One uncosted product, 5 units, worth £100 at retail.
Rule 3: aging is measured from the last real sale — Full edition
A product listed three years ago that sells every week is not old stock. Aging by creation date lists a shop's oldest products rather than its stuck money, so the full edition ages stock from the last time a unit actually sold.
- Read the orders of the counted statuses placed within the look-back (730 days by default), a page at a time.
- For each product, keep the most recent date it appeared on such an order. A variation that counts its own stock is its own product here; a sale of a variation whose stock is kept on its variable product counts as a sale of that product.
- A product with no sale in that time has never sold, as far as the report knows. It is aged from the day it was listed — the only honest fallback — and marked "never sold", because "never sold since March" and "last sold in March" are different facts.
- Put each product with stock on hand into a bucket by days since that date: Under 30 days, 31 to 60 days, 61 to 90 days, 91 to 180 days, Over 180 days. Money that is not on the shelf is not stuck on it, so a product with no stock is in no bucket.
Dead stock — Full edition
Products with stock on hand and nothing sold for 181 days or more, dearest first, with the number of days and the value. The aging pass reads the whole counted shelf, not just the rows shown, because a shop's dead stock is very often the tail a short table would have cut off.
By category — Full edition
The same shelf grouped by product category: products, units, value at cost and value at retail. A variation is filed under its parent's categories, which is where the merchant filed it. A product in two categories is counted in both, so the category totals do not add up to the shop's total — "how much is sitting in Frozen?" means everything filed there.
When the numbers are worked out
Every time the screen is opened (and when the full edition's CSV is downloaded). Nothing is cached and nothing runs on a schedule, so the figures are always the shop's stock as it is now.
