order_return_stock_counts_before

Triggered before returning the quantities of ordered product variants to stocks when an ordered is deleted or refunded. 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 returned to stocks. Can be modified in the event handler before saving.
$params['counts'][sku_id][stock_id] float The quantity of the product variant with sku_id to be returned to stock stock_id.
… your plugin code …

Output

Shop-Script

Plugin code example

PHP

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

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

    $log_text = sprintf(
        'Values to return 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_return_stock_counts_before.log'
    );
}