WooCommerce Filter by Size Shows Out-of-Stock Variations: How to Fix
A shopper ticks size M, and the WooCommerce filter by size shows a T-shirt whose M variation is out of stock. The product page then says the size is sold out, and the shopper leaves. The filter is not broken: by default it judges a variable product by its parent product, exactly as WooCommerce does, and the parent stays in stock while any of its variations is. Below is why the filter shows out-of-stock variations, the HUSKY setting that makes it check the stock of the selected size, the rules that check follows, and what to fix when results or counts are still wrong.
Why the filter by size shows sold-out variations
In WooCommerce a variable product is two kinds of records. The parent product is what shop and category pages list. Its Attributes tab holds the values (Size: S, M, L; Colour: black, red), and these values are attached to the parent as attribute terms. Each variation, such as M / black, is a separate record with its own price and its own stock.
A filter by attribute looks for parent products that have the selected term. If the T-shirt lists M in its Attributes tab, it matches size M, whatever the stock of the M variation. The parent's stock status does not help either: WooCommerce keeps it at In stock while at least one variation is in stock (or On backorder if one is on backorder) and switches it to Out of stock only when every variation is sold out. For the same reason the WooCommerce option Hide out of stock items from the catalog (WooCommerce → Settings → Products → Inventory) does not solve this: it hides the T-shirt only when all its sizes are gone. The question "is size M available?" has to be asked of the variations, and the filter asks it only when you tell it to.
Make HUSKY check the stock of the selected size
The variation check belongs to the In stock element. In HUSKY 3.4.6 it is a built-in extension that is always active, so there is nothing to switch on in the Extensions tab.
- Open WooCommerce → Settings → Products Filter, tab Structure, and find the In stock row. Set it to Yes if shoppers should see the checkbox.
- Click the gear button next to it (additional options). In the window that opens, set the first setting, Search in variable produts, to Search in products and their variations. The default, Simple products only, looks only at each product's own stock status, which for a variable product is the parent's.
- Leave Use new DB. at Yes and pick Checkbox or Switcher under View.
- Save, clear the page cache and test: choose a size that you know is sold out in one product, tick In stock, and that product should disappear from the results.
The check runs only while In stock is part of the search and at least one product attribute (a pa_ taxonomy such as Size or Colour) is selected. Without In stock, attribute filtering stays parent-based. The setting costs extra database work, which is why it is off by default; see the section on speed below.
Apply In stock without a checkbox
If sold-out sizes should never match, you do not have to rely on shoppers ticking the box. The woof_get_request_data filter can add the in-stock condition to every search the plugin runs:
/*
* HUSKY: always search in-stock products only.
* Child theme functions.php or a code snippets plugin.
*/
add_filter('woof_get_request_data', function ($data) {
$data['stock'] = 'instock';
return $data;
});
This also removes out-of-stock simple products and fully sold-out variable products from every product list the filter controls, as described in the FAQ on showing only in-stock products. Set the In stock row to No, so shoppers do not see a checkbox that is always ticked.
If out-of-stock products should stay visible while shoppers browse, and drop out only once they filter by an attribute, add the condition only when an attribute is in the search:
/*
* HUSKY: apply In stock only when a product attribute (pa_*) is being filtered.
* Child theme functions.php or a code snippets plugin.
*/
add_filter('woof_get_request_data', function ($data) {
foreach (array_keys($data) as $key) {
if (strpos($key, 'pa_') === 0) {
$data['stock'] = 'instock';
break;
}
}
return $data;
});
How the variation check decides
With Use new DB. set to Yes, HUSKY reads the WooCommerce product attributes lookup table, where every variation has a row for each of its attribute values together with an in-stock flag. For each variable product it collects the variations that carry any of the selected values, and removes the product when such variations exist and none of them is in stock. This rule explains the results that look odd at first:
| Product | Search | Result |
|---|---|---|
| S in stock, M sold out | Size M + In stock | Hidden |
| S in stock, M sold out | Size S or M + In stock | Shown: S is available |
| M listed in the Attributes tab, but no M variation exists | Size M + In stock | Shown: no M variation can be out of stock |
| One variation set to Any Size, in stock | Size L + In stock | Shown: an Any Size variation counts for every size listed on the product |
| M / black sold out, M / red and S / black in stock | Size M + Colour black + In stock | Shown: each selected value has an in-stock variation |
| M variation On backorder | Size M + In stock | Shown: backorder counts as available |
The third row is a data problem, not a filter problem. A value that stays in the parent's Attributes tab after you deleted its variation, or that was never used for a variation, keeps matching and keeps being counted. Keep only the values that have variations, or create the variation and mark it Out of stock.
The last two rows are the limits of the check. Values of different attributes are checked one by one, not as the exact combination the shopper picked, so a product can still appear when that one combination is sold out. Variations on backorder are purchasable, so In stock keeps them; if they should not match either, add the Exclude products on backorder element, which has the same Search in products and their variations setting. Also check what an import actually wrote: a variation saved as On backorder instead of Out of stock stays in the results.
Results still wrong: refresh the WooCommerce lookup table
The lookup table (wc_product_attributes_lookup) belongs to WooCommerce. It is updated when a product or variation is saved through WooCommerce, by default with a short delay: WooCommerce schedules the update as a background action. The table falls behind when:
- stock is changed by an import or a sync tool that writes straight to the database instead of saving products through WooCommerce;
- background actions do not run, for example because WP-Cron is disabled without a server cron to replace it. Pending actions with the hook
woocommerce_run_product_attribute_lookup_update_callbackthen pile up under WooCommerce → Status → Scheduled Actions; - a full regeneration of the table was interrupted.
The filter then works with old stock data, and the results look random. To rebuild the table, open WooCommerce → Status → Tools and click Regenerate next to Regenerate the product attributes lookup table. It runs in the background in batches; the button shows Filling in progress with a counter until it is done. The FAQ on price and stock filters not working covers the same tools. If stock changes often, consider WooCommerce → Settings → Products → Advanced → Product attributes lookup table → Direct updates, which updates the table at the moment a product is saved.
As a test, or when the table cannot be kept current, set Use new DB. to No. HUSKY then reads the variations themselves (their _stock_status and attribute_pa_… values). This is slower with many variations and follows a stricter rule: a product is removed when any variation with a selected value is out of stock, so "Size S or M" hides the T-shirt from the table above. To get the same rule as with the lookup table, add:
/*
* HUSKY, In stock with "Use new DB." = No: keep a variable product when at least
* one variation with a selected value is in stock.
* Child theme functions.php or a code snippets plugin.
*/
add_filter('woof_exclude_existing_variations', '__return_true');
Counts that include out-of-stock variations
The numbers next to the sizes come from the same queries as the results, so they follow the same rules.
- Without In stock in the search, the count for M includes every product that has M on its parent, sold out or not. With In stock active (ticked, or forced with the snippet above) and variation search on, every size is counted with the variation check for that size.
- Add Dynamic recount and Hide empty terms (tab Options) to that, and a size whose count drops to zero disappears from the filter, so sold-out sizes are no longer offered at all.
- If WooCommerce hides out-of-stock items, keep Listen catalog visibility (Advanced → Options) at Yes. With No, the counters also include the products WooCommerce hides, and a term can show a count while its results are empty.
- With Cache dynamic recount number for each item in filter (Advanced → Options), counts are stored and do not follow stock changes until the cache is cleaned automatically or with the clear cache button. A page cache can serve old numbers as well.
Speed: the price of checking variations
Every search that combines an attribute with In stock runs extra queries: two on the lookup table with Use new DB. at Yes, or one over all matching variations with No. With Dynamic recount the check also runs for every value that gets a count, so a filter with 40 sizes and colours repeats it 40 times per page. With tens of thousands of variations this is noticeable.
- Keep Use new DB. at Yes and the lookup table current; it is the faster method.
- Enable Cache dynamic recount number for each item in filter and pick an auto-clean interval that matches how quickly your stock changes.
- More options are in how to speed up product filtering in large catalogs.
Turbo mode is not a shortcut here. It filters in the visitor's browser from a prebuilt file, and its In stock check looks only at each product's own stock status: the variation setting has no effect there, and stock changes appear only after the file is rebuilt. If sold-out sizes must not match, keep Turbo mode off; its other limits are listed in Turbo mode troubleshooting.
Checklist
- In stock → gear → Search in products and their variations selected and saved.
- In stock is part of the search: ticked by the shopper or forced with
woof_get_request_data. - Variable products list only the attribute values that have variations.
- Sold-out variations have the status Out of stock, not On backorder.
- The lookup table was regenerated after imports, and no lookup updates are stuck in Scheduled Actions.
- Recount cache and page cache cleared after stock changes; Turbo mode off.
If the filter still returns a product whose selected size is sold out, open a topic on the HUSKY support forum with the filter link, the product, and the stock status of its variations.