Stockpool documentation1.x
Developers

Filters and hooks

Change the stock text and emails, add pools to products from code, and plug your own boxes into the pool editor.

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

stockpool_stock_changed

Fires after every change to a pool's stock, with the pool ID, the new stock, the old stock, the reason (order, restore, refund, manual, api, purchase, …) and the order ID, if any.

// Tell a warehouse system.
add_action( 'stockpool_stock_changed', function ( $pool_id, $new, $old, $reason, $order_id ) {
	wp_remote_post( 'https://warehouse.example/api/stock', array(
		'body' => array( 'pool' => $pool_id, 'stock' => $new ),
	) );
}, 10, 5 );

stockpool_product_pools

The pools a product or variation uses: pool ID => amount per item. Starts with the product's own links; add or remove pools here. The third argument holds the shopper's attribute choices for variations set to "Any".

// Every product in the "Gift wrapped" category also takes one gift box.
add_filter( 'stockpool_product_pools', function ( $pools, $product, $chosen ) {
	$id = $product->get_parent_id() ?: $product->get_id();
	if ( has_term( 'gift-wrapped', 'product_cat', $id ) ) {
		$box = \Extendiment\Stockpool\Pools::find_by_sku( 'BOX-KRAFT' );
		if ( $box ) {
			$pools[ $box->id ] = $pools[ $box->id ] ?? 1.0;
		}
	}
	return $pools;
}, 10, 3 );

If you add pools this way, tell Stockpool which products to check when the pool changes with stockpool_pool_products (a list of product IDs that may use the pool), so their stock status stays right.

stockpool_availability_text

The stock text for a product that uses pools. Gets the text, the units the pools cover, and the product.

add_filter( 'stockpool_availability_text', function ( $text, $units, $product ) {
	return $units <= 3 ? sprintf( 'Last %d, cut to order', $units ) : $text;
}, 10, 3 );

stockpool_stock_email

The low stock or out of stock email: an array of recipient, subject and message, and the pool. Return false to skip it.

add_filter( 'stockpool_stock_email', function ( $email, $pool ) {
	$email['recipient'] = '[email protected]';
	return $email;
}, 10, 2 );

stockpool_unit_singular

The singular of plural units, used when an amount is exactly 1 ("1 box", not "1 boxes"). Starts with the plural units the editor suggests (rolls, sheets, boxes) and kits; add your own.

add_filter( 'stockpool_unit_singular', function ( $singular ) {
	$singular['spools'] = 'spool';
	return $singular;
} );

The pool editor

  • stockpool_pool_editor_boxes (action, with the pool) prints more boxes inside the editor's form; use \Extendiment\Stockpool\Admin\PoolsPage::box() for WordPress's postbox markup. Read your fields on stockpool_pool_saved while admin_post_stockpool_save runs, or change the pool before it's saved with stockpool_pool_from_form (return a WP_Error to refuse the save with its message). Supplyline's Purchasing box works this way.
  • stockpool_pool_editor_stock (action) adds controls under the stock adjustment.
  • stockpool_pool_editor_glance (filter) changes the facts in the box on the right.

More

Stockpool Pro uses the same hooks, and a few more for its rules and kits; the plugin's README lists every one. stockpool_loaded fires once Stockpool has loaded, for add-ons.