# Filters and hooks - Stockline 1.x documentation

> Change the markup, add placeholders, load the stylesheet elsewhere, or give products their own settings from code.

Stockline's hooks are WordPress filters and actions. Put the examples in a small custom plugin or your child theme's `functions.php`.

## The markup

The stock line is a paragraph that keeps WooCommerce's classes and adds Stockline's own:

```html
<p class="stock out-of-stock stockline stockline--out-of-stock stockline--badge stockline--colored stockline--color-cf222e">
  <span class="stockline__icon" aria-hidden="true"><svg>…</svg></span>
  <span class="stockline__text">Sold out, back soon</span>
</p>
```

- `stockline--{state}`: `in-stock`, `low-stock`, `backorder`, `out-of-stock`, or an added state's key.
- `stockline--plain` or `stockline--badge`: the style.
- `stockline--colored` and `stockline--color-{hex}`: only when the state has a colour. The colour class sets the CSS variable `--stockline-color`.

So you can restyle it with CSS alone, for example a squarer badge:

```css
.stockline--badge { border-radius: 4px; }
```

## stockline_stock_html

Filters the final markup. Gets the HTML, the product (or variation) and the state.

```php
// Add a "Notify me" link under out of stock messages.
add_filter( 'stockline_stock_html', function ( $html, $product, $state ) {
	if ( 'out_of_stock' === $state ) {
		$html .= '<p class="notify-me"><a href="/notify?product=' . $product->get_id() . '">Email me when it is back</a></p>';
	}
	return $html;
}, 10, 3 );
```

The states are `in_stock`, `low_stock`, `backorder` and `out_of_stock` (with underscores here), plus any added with `stockline_states`.

## stockline_placeholders

Adds placeholders for messages. Gets the placeholder => value pairs and the product. Values are inserted as HTML, so escape anything that comes from user input.

```php
// {sku} in any stock message.
add_filter( 'stockline_placeholders', function ( $pairs, $product ) {
	$pairs['{sku}'] = esc_html( $product->get_sku() );
	return $pairs;
}, 10, 2 );
```

## stockline_enqueue_styles

Stockline's stylesheet loads on single product pages only. Return `true` where products are shown some other way, so the icons, colours and badges are styled there too.

```php
// A landing page with a Single Product block.
add_filter( 'stockline_enqueue_styles', function ( $enqueue ) {
	return $enqueue || is_page( 'spring-collection' );
} );
```

## stockline_state

Changes the state a product is shown in. Only Stockline's four states or one added with `stockline_states` are accepted; anything else is ignored. See **Adding your own stock state** for a full example.

## stockline_product_settings

Replaces the store's settings for one product or variation. Gets an array with `message`, `icon` and `color`, the product, and the state. An empty message keeps WooCommerce's text, `none` means no icon, and an empty colour keeps the theme's. Stockline checks the result: an unknown icon becomes `none` and an invalid colour is dropped.

```php
// A different backorder message for one category.
add_filter( 'stockline_product_settings', function ( $settings, $product, $state ) {
	$id = $product->get_parent_id() ?: $product->get_id();
	if ( 'backorder' === $state && has_term( 'furniture', 'product_cat', $id ) ) {
		$settings['message'] = 'Made to order, ships in 6–8 weeks';
		$settings['icon']    = 'truck';
	}
	return $settings;
}, 10, 3 );
```

Stockline Pro uses this filter for its per-product and per-variation settings. If you use both, run yours at a later priority (e.g. 20) to override Pro, or an earlier one to let Pro's product settings win.

## stockline_page_colors

If your `stockline_product_settings` code sets a colour that isn't one of the store's, add it here so its CSS rule is in the page head. Gets the list of hex colours and the page's product (`null` when the page isn't a product page).

```php
add_filter( 'stockline_page_colors', function ( $colors, $product ) {
	$colors[] = '#8250df';
	return $colors;
}, 10, 2 );
```

Colours that aren't listed still work on most pages: Stockline adds their rule in the footer when it first meets them. Listing them avoids a moment without colour on classic themes, and covers variations loaded after the page.

## stockline_loaded

An action that fires once Stockline has loaded (on `plugins_loaded`, and only when WooCommerce is active). Hook an add-on's setup to it, so it only runs when Stockline is there.

```php
add_action( 'stockline_loaded', function () {
	// Stockline's classes, such as Extendiment\Stockline\States, are available here.
} );
```

`STOCKLINE_VERSION` is defined from then on, for version checks.

Source: https://www.extendiment.com/plugins/stockline/docs/1.x/filters-and-hooks
