order_reduce_stock_counts_before

Triggered after reducing the ordered product variants’ counts during the order processing. Allows tracking changes to the stock inventory; e.g., to sync data with online marketplaces.

Shop-Script

Input (passed by reference)

$params['order_id'] int Order ID.
$params['counts'] array Information about the quantities of ordered product variants which are about to be written off from the stocks. Can be modified in the event handler before saving.
$params['counts'][sku_id][stock_id] float The quantity of product variant with sku_id to be written off from stock stock_id.
… your plugin code …

Output

Shop-Script

Plugin code example

PHP

public function orderReduceStockCountsBefore($params)
{
    $log_strings = [];

    foreach ($params['counts'] as $sku_id => $sku_quantities) {
        foreach ($sku_quantities as $stock_id => $sku_stock_quantity) {
            $log_strings[] = sprintf(
                '- write off %0.3f of sku #%d from stock #%d',
                abs($sku_stock_quantity),
                $sku_id,
                $stock_id
            );
        }
    }

    $log_text = sprintf(
        'Values to reduce the quantities of product variants before processing order #%d:%s',
        $params['order_id'],
        PHP_EOL . implode(PHP_EOL, $log_strings)
    );

    waLog::log(
        $log_text,
        'shop/plugins/' . $this->getId() . '/order_reduce_stock_counts_before.log'
    );
}