This developer reference for Stock History & Reports Manager for WooCommerce covers the actions, filters, scheduled action hooks, shortcode and stored data you can use in custom code. Settings are at WPFactory > Stock Snapshot. All hooks exist in both the free and Pro versions.
Actions #
| Action | Parameters | When it fires |
|---|---|---|
alg_wc_stock_snapshot_take_snapshot_product |
$product_id, $product, $stock, $index, $prev_stock |
After a snapshot of one product is saved. Fires for every snapshot, including product update snapshots. $prev_stock is '-' for a first snapshot. |
alg_wc_stock_snapshot_take_snapshot_start |
None | Before a full snapshot (periodic, manual or URL) starts. |
alg_wc_stock_snapshot_take_snapshot_end |
None | After a full snapshot finishes. |
alg_wc_stock_snapshot_core_loaded |
None | After the plugin core is loaded. |
alg_wc_stock_snapshot_settings_saved |
None | When the plugin settings are saved, before a section reset. The Tools run on this action. |
alg_wc_stock_snapshot_after_settings_saved |
None | After the settings are saved and any section reset has run. |
Example: log large stock drops #
add_action( 'alg_wc_stock_snapshot_take_snapshot_product', function ( $product_id, $product, $stock, $index, $prev_stock ) {
if ( '-' !== $prev_stock && null !== $stock && ( $prev_stock - $stock ) >= 50 ) {
wc_get_logger()->warning(
sprintf( 'Stock for product #%d dropped from %s to %s', $product_id, $prev_stock, $stock ),
array( 'source' => 'my-stock-alerts' )
);
}
}, 10, 5 );
Filters #
| Filter | Parameters | Use |
|---|---|---|
alg_wc_stock_snapshot_request_uri_desc |
$desc (default false), $data (snapshot array), $product_id, $is_short |
Return a string to replace the Context description for a snapshot, for example to label changes from your own stock tool. Return false to keep the default. |
alg_wc_stock_snapshot_report_product_name_add_category_list |
$add (default false) |
Return true to add the product’s categories after the product name in the Report section and report emails. |
// Show categories next to product names in the Report.
add_filter( 'alg_wc_stock_snapshot_report_product_name_add_category_list', '__return_true' );
// Label snapshots made by a custom stock sync endpoint.
add_filter( 'alg_wc_stock_snapshot_request_uri_desc', function ( $desc, $data, $product_id, $is_short ) {
if ( isset( $data['request_uri'] ) && 0 === strpos( $data['request_uri'], '/wp-json/my-sync/' ) ) {
return $is_short ? 'stock sync' : 'external stock sync';
}
return $desc;
}, 10, 4 );
Scheduled action hooks (Action Scheduler) #
| Hook | Purpose |
|---|---|
alg_wc_stock_snapshot_action |
Recurring periodic snapshot. Its argument is the interval in seconds. |
alg_wc_stock_snapshot_report_action |
One-off job that prepares Report data when Prepare reports in background is on. |
alg_wc_stock_snapshot_report_email_action |
Daily recurring report email (Pro), one per Email data line. |
You can inspect all three at WooCommerce > Status > Scheduled Actions.
Taking a snapshot from code #
// Full snapshot of all products. Returns the number of products recorded.
$count = alg_wc_stock_snapshot()->core->take_stock_snapshot( false );
// Snapshot of a single product.
alg_wc_stock_snapshot()->core->take_product_stock_snapshot( $product_id );
Pass false to take_stock_snapshot(). With the default true, it stops the request after running, as the URL snapshot does.
Shortcode #
[alg_wc_stock_snapshot_restocked] accepts min_stock_diff (1), total_snapshots (1), new_stock (no), columns (4), paginate (no), orderby (name, or last_restocked), order (ASC) and not_found_msg. It outputs a WooCommerce [products] grid. Full details are in How to Show Recently Restocked Products in WooCommerce.
Stored data #
- Snapshots are stored in post meta
_alg_wc_stock_snapshoton each product and variation, as an array keyed by Unix timestamp. - With Extra data off, each value is the stock quantity. With Extra data on, each value is an array with
stock,hook,user_idand, when available,request_uri,action,post_typeandorder_id. - Transients:
alg_wc_stock_snapshot_restocked,alg_wc_stock_snapshot_historyandalg_wc_stock_snapshot_report_data. They have no expiry and are deleted whenever a snapshot is taken or settings are saved. - Settings are WordPress options prefixed
alg_wc_stock_snapshot_. See the settings reference.
Need a hand? Our team answers every ticket on the Stock History support forum.