ThisCalculator

Everyday

WooCommerce Sold Out Archive: Block & Store API

Updated · Standard formula, shown below

To build a WooCommerce “sold out” product archive, you combine a custom PHP block (or a shortcode) that queries products with zero stock, and then render that data using the WooCommerce Store API for dynamic, client-side updates. The Store API provides the endpoints to fetch product data (including stock status) as JSON, while your PHP code defines the query logic and the block’s server-side rendering.

Background: Why a Sold Out Archive Exists

Most WooCommerce stores hide out-of-stock products by default (under WooCommerce > Settings > Products   Inventory). This is good for conversion—nobody wants to click a product they can’t buy. But there are legitimate reasons to show them: pre-orders, back-in-stock notifications, showcasing a full catalog, or letting customers see what’s popular. A dedicated “sold out” archive gives you a curated page of those unavailable items without changing your global visibility settings.

The challenge is that WooCommerce doesn’t ship a built-in archive for sold-out products. You have to build it. The two main approaches are a classic PHP template with WP_Query, or a block that uses the Store API. The latter is more modern and plays nicely with the block editor.

How the Store API Fits In

The WooCommerce Store API (introduced in WooCommerce 8.0) is a set of REST endpoints under /wp-json/wc/store/v1/. It’s designed for headless and block-based frontends. For a sold-out archive, the relevant endpoint is:

  • /wp-json/wc/store/v1/products – returns a list of products with fields like id, name, price, stock_status, and stock_quantity.

You can filter this endpoint with query parameters. For example, ?stock_status=outofstock will return only products marked as out of stock. The endpoint supports pagination (page, per_page), sorting (orderby), and category filtering (category).

To use it inside a block, you’d write JavaScript that fetches from this endpoint and renders the results. However, the Store API is public—it doesn’t require authentication for GET requests—so anyone can call it directly. That’s fine for a public archive, but be aware it exposes your product data (which is already public on the product pages anyway).

Building the PHP Block (Server-Side)

A simpler, more reliable approach for most stores is a custom block that renders server-side. You register a block type in PHP, and in its render_callback function you run a WP_Query to fetch sold-out products, then output the HTML. Here’s the skeleton:

  1. Register the block with register_block_type, pointing to a render callback.
  2. Query products with wc_get_products() or WP_Query, filtering by stock_status = 'outofstock'.
  3. Loop and output product cards (image, title, price, “Sold Out” badge).
  4. Cache the output with wp_cache or transient to avoid hammering the database on every page load.

Example query inside the callback:

$products = wc_get_products(array(
    'status' => 'publish',
    'stock_status' => 'outofstock',
    'limit' => 12,
));

This returns an array of WC_Product objects. You then loop through them and build your HTML. This method is fast, works with caching plugins, and doesn’t require a client-side fetch.

Choosing Between PHP and Store API

Which method should you use? It depends on your needs:

  • Pure PHP block – Best for SEO (content is in the initial HTML), works without JavaScript, and is simpler to debug. Use this if your store is traditional and you want the archive indexed by search engines.
  • Store API + JavaScript – Best for dynamic updates (e.g., live stock changes without a page reload), or if you’re already building a headless frontend. It’s more complex and requires handling loading/error states.
  • Hybrid – Render the first page server-side for SEO, then use the Store API for pagination or filtering via AJAX. This is the best of both worlds but takes more code.

For most store owners, the pure PHP block is the right choice. It’s less code, more robust, and doesn’t depend on the client’s JavaScript execution.

Common Problems and Pitfalls

Here are the issues you’ll likely hit:

  • Out-of-stock products hidden globally – If your settings hide out-of-stock items, your query might still return them (since the query bypasses the frontend filter), but the product pages themselves will 404. You may need to override this for the archive page.
  • Backorders – A product can be out of stock but allow backorders. Decide whether to include it. Filter on stock_status gives you out-of-stock only; to exclude backorderable items, check $product->backorders_allowed().
  • Variable products – A variable product’s stock status is often based on its variations. A parent product can be out of stock while some variations are in stock. Use WC_Product_Query carefully; you may need to loop variations.
  • Performance – Querying all sold-out products on a large catalog can be slow. Add pagination and limit the query. Consider a transient cache that refreshes when stock changes (hook into woocommerce_product_set_stock).
  • Pagination with the Store API – The endpoint returns a X-WP-Total header, but if you’re fetching client-side, you must manage page state yourself. The block editor’s useSelect hook can help.

Costs and Alternatives

Building this yourself costs only development time. If you’d rather not code, there are alternatives:

  • Plugins – Some catalog/archive plugins let you create custom product lists, but few specifically target sold-out items. Check the plugin’s query builder before buying.
  • Shortcode with a custom query – A simple [sold_out_products] shortcode is easier to maintain than a full block, and works in any post or page.
  • WooCommerce product visibility – Instead of a separate archive, you could create a product category called “Sold Out” and assign products to it. This is manual but requires zero code.

There’s no ongoing cost to the Store API approach—it’s part of WooCommerce core. But you’ll pay in maintenance if you don’t cache properly.

Safety and Best Practices

A few important notes:

  • Don’t expose unpublished products – Always filter by status => 'publish' in your query. The Store API already does this by default.
  • Escape output – Use esc_html() and esc_url() when printing product names and image URLs to prevent XSS.
  • Respect the woocommerce_rest_check_permissions filter – If you extend the Store API with custom endpoints, make sure you’re not leaking data you shouldn’t.
  • Test with a staging site – A query that accidentally returns thousands of products can slow your site. Always paginate.

One misconception: the Store API is not faster than WP_Query. It’s a REST API, so it has HTTP overhead. For a server-rendered block, direct PHP queries are faster. The Store API’s advantage is decoupling—not speed.

Frequently Asked Questions

Can I use the Store API without a block?
Yes. You can call the endpoint from any JavaScript on the page, or even from a custom admin screen. The block is just a convenient wrapper.

How do I show “Sold Out” on the product card?
In your loop, check $product->is_in_stock(). If false, output a badge. For variable products, check if all variations are out of stock using $product->get_available_variations().

Will this work with caching plugins?
Yes, but you must ensure your block output is cached. Use the block’s render callback to generate HTML, and let the page cache handle the rest. Avoid client-side fetching if you use full-page caching, or the cache won’t capture the dynamic content.

What if I want to include products that are “on backorder”?
By default, stock_status => 'outofstock' excludes backorderable items. To include them, query all products and filter with PHP: if ($product->is_on_backorder()).

Is there a way to do this without any code?
Use a product category named “Sold Out” and manually assign products. It’s not automatic, but it requires no development. Some third-party plugins also offer “show out-of-stock” toggles per category.

Does the Store API support sorting by stock status?
No. You can filter by stock_status, but you can’t sort by it. You’ll need to sort by date, price, or title, then filter. If you need stock-based sorting, do it in PHP with a custom WP_Query.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.