Stockline documentation1.x
Developers

Adding your own stock state

Add a state such as "Discontinued" or "Pre-order" with its own message, icon and colour on the settings screen.

Beyond in stock, low stock, on backorder and out of stock, a plugin can add states of its own. An added state gets a message, icon, colour and preview on the Stock messages screen like the built-in ones, so the wording stays in the store owner's hands.

It takes two filters: stockline_states to describe the state, and stockline_state to put products in it.

Example: Discontinued

Products tagged discontinued get their own state, with a grey "No longer made" message.

add_filter( 'stockline_states', function ( $states ) {
	$states['discontinued'] = array(
		'label'       => 'Discontinued',                   // Heading on the settings screen.
		'placeholder' => 'e.g. No longer made, see {alternative}', // Hint in the empty message field.
		'icon'        => 'dot',                            // Default icon.
		'color'       => '#6e7781',                        // Default colour ('' for the theme's).
		'sample'      => 'Out of stock',                   // Preview text while the message is empty.
		'class'       => 'out-of-stock',                   // WooCommerce's stock class for the preview.
		'pairs'       => array( '{alternative}' => 'the Oak Board' ), // Sample placeholder values for the preview.
	);
	return $states;
} );

add_filter( 'stockline_state', function ( $state, $product ) {
	$id = $product->get_parent_id() ?: $product->get_id();
	return has_term( 'discontinued', 'product_tag', $id ) ? 'discontinued' : $state;
}, 10, 2 );

// Optional: a placeholder for the new state's message.
add_filter( 'stockline_placeholders', function ( $pairs, $product ) {
	$pairs['{alternative}'] = '<a href="/product/oak-board/">the Oak Board</a>';
	return $pairs;
}, 10, 2 );

After adding this, WooCommerce → Settings → Products → Stock messages has a Discontinued section after Out of stock. The store owner writes the message there; until they do, the product shows WooCommerce's own text with the state's icon and colour.

The state definition

Key Required Meaning
label Yes The state's name, shown as the section heading. A state without one is ignored.
placeholder No Example text in the empty message field.
icon No Default icon: check, alert, clock, cross, truck, dot or none (the default).
color No Default hex colour; empty for the theme's colour.
sample No What the settings preview shows while the message is empty, standing in for WooCommerce's text. Defaults to the label.
class No WooCommerce's stock class to use in the preview, e.g. in-stock (the default) or out-of-stock.
pairs No Sample values for your placeholders, used in the preview only.

Rules

  • The key is cleaned with sanitize_key() (lowercase letters, numbers, _ and -) and becomes part of the option names: stockline_discontinued_message, _icon and _color.
  • The four built-in states can't be replaced or removed.
  • stockline_state must return your state's key exactly. An unknown state is ignored and the product keeps the state Stockline worked out.
  • On the product page, the line keeps the WooCommerce class of the product's real stock (for example in-stock), and adds stockline--discontinued.

Preorderline adds its Pre-order state this way, with a {date} placeholder for the release date.

Note

Stockline Pro's per-product fields and preview dialog cover the four built-in states. Products in an added state use the store-wide message for it, unless your own code sets one with stockline_product_settings.