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']
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]', array(
        'options' => array(
            10 => _wp('10+ items'),
            5 => _wp('6–10 items'),
            1 => _wp('1–5 items'),
        ),
        'title' => $title,
        'control_wrapper' => '<p>%s%s</p>'
    ));

    $html = <<<HTML
<script type="text/template" id="{$this->id}_condition_template">
    {$select}
</script>
<script>
(function() {
    var template_form = $('#{$this->id}_condition_template').html();
    var table_tbody = $('#s-settings-stock-rules-form table tbody').first();
    table_tbody.on('rules:condition_init.{$this->id}', '.stock-rule-condition', function(evt) {
        var condition_wrapper = $(this);
        var tmpl = template_form.replace(/%ID%/g, evt.rule_id);
        condition_wrapper.find('input[name$="[rule_data]"]').replaceWith($.parseHTML(tmpl));
        condition_wrapper.find('select[name$="[rule_data]"]').val(evt.rule_data);
    });
})();
</script>
HTML;

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