backend_settings_stocks

Adds custom conditions for stock selection rules setup interface. A plugin must return an array of sub-arrays with the following keys: 'id' (string) — rule ID to be saved in 'rule_type' field of 'shop_stock_rules' table in “[plugin ID]_[rule ID]” format; 'name' (string) — localized condition name; 'init_html' (string) — HTML code to be added to a page as a hidden block, it can be useful to embed custom JavaScript. See also the description of 'frontend_checkout_stock_rules' hook on how stock selection rules are applied in the frontend.

Shop-Script

Input (passed by reference)

$params array Parameters.
$params['rules'] array Rules from 'shop_stock_rules' table.
$params['stocks'] array Stocks returned by shopHelper::getStocks() method.
… your plugin code …

Output

$return['custom_html']
$return['id']
$return['name']
$return['init_html']
Shop-Script

Plugin code example

PHP

/**
 * As an example, let us define limits for quantities of ordered items
 * each automatically selecting one of available stocks
 */
public function backendSettingsStocks($params)
{
    $title = _wp('By ordered product quantity');

    $select = waHtmlControl::getControl(waHtmlControl::SELECT, 'rules[%ID%][rule_data]', [
        'options' => [
            10 => _wp('10+ items'),
            5 => _wp('6–10 items'),
            1 => _wp('1–5 items'),
        ],
        'title' => $title,
        'control_wrapper' => '<div class="wide">%s%s</div>'
    ]);

    $html = <<<HTML
<script id="{$this->id}_condition_template">
    {$select}
</script>

<script>
    (function() {
        const template_form = $('#{$this->id}_condition_template').html();
        const table_tbody = $('#s-settings-stock-rules-form table tbody').first();

        table_tbody.on('rules:condition_init.{$this->id}', '.stock-rule-condition', function(event) {
            const condition_wrapper = $(this);
            const template = template_form.replace(/%ID%/g, event.rule_id);

            condition_wrapper.find('input[name$="[rule_data]"]').replaceWith($.parseHTML(template));
            condition_wrapper.find('select[name$="[rule_data]"]').val(event.rule_data);
        });
    })();
</script>
HTML;

    return [
        'id'          => $this->id,
        'name'        => $title,
        'init_html'   => $html,
        'custom_html' => '<script>/* E.g., some additional JavaScript snippet */</script>'
    ];
}