backend_prod_presave

Triggered before a product is saved in the new product editor. Allows changing of product properties before they are saved or returning an error message, which will interrupt the saving of a product and will show the error message to a user. If necessary, you can change the way your error messages are displayed by registering a handler for the wa_after_save event in plugin’s JavaScript code.

Shop-Script

Input (passed by reference)

$params['product'] shopProduct Object of product properties before they have been modified by saving.
$params['data'] array Product data to be saved. The data content in various product editor tabs may differ. Available by reference for modification.
$params['content_id'] string Identifier of the current product editor tab.
… your plugin code …

Output

$return['errors']

Массив сообщений об ошибках.
$result['errors']['text'] string Error message text.

Shop-Script

Plugin code example

PHP

public function backendProdPreSave($params)
{
    /** @var shopProduct $product */
    $product = $params['product'];
    $content_id = $params['content_id'];

    $errors = [];

    if ($content_id == 'sku') {
        $data = $params['data'];
        foreach(ifset($data, 'skus', []) as $sku_id => $sku) {
            // Additional SKU price fields validation
            $price_field_id = 'myplugin_price';

            if (ifset($sku, 'additional_prices', $price_field_id, 0) < 10) {
                $errors[] = [
                    'id' => 'plugin_price_error',
                    "name" => "product[skus][{$sku_id}][additional_prices][{$price_field_id}]",
                    'text' => _wp('My plugin’s price value must be equal to or greater than 10.'),
                    'data' => [
                        'sku_id' => (int) $sku_id,
                        'sku_sku' => $sku['sku'],
                        'field_id' => $price_field_id,
                    ],
                ];
            }

            // Other additionl SKU fields validation
            $textarea_field_id = 'myplugin_textarea';

            if (mb_strlen(ifempty($sku, 'additional_fields', $textarea_field_id, '')) < 2) {
                $errors[] = [
                    'id' => 'plugin_field_error',
                    "name" => "product[skus][{$sku_id}][additional_fields][{$textarea_field_id}]",
                    'text' => _wp('My plugin’s textarea value must be 2 or more characters long.'),
                    'data' => [
                        'sku_id' => (int) $sku_id,
                        'sku_sku' => $sku['sku'],
                        'field_id' => $textarea_field_id,
                    ],
                ];
            }

        }
    }

    if ($content_id == 'general' || $content_id == 'sku') {
        $myplugin_post_values = waRequest::post('myplugin', [], waRequest::TYPE_ARRAY_TRIM);
        $some_field_value = ifset($myplugin_post_values, 'some_value', '');

        if (!strlen($some_field_value)) {
            // Each error is available as an object in the event.server_errors array
            // in a 'wa_after_save' event handler for '#js-product-general-section' and similar elements.

            // You can arbitrarily use 'id', 'plugin', and 'name' values of each error in your JavaScript code
            // to show its message in a custom way instead of the default error block at the page bottom.

            // 'id' value must always be 'plugin_error'
            $errors[] = [
                'id'   => 'plugin_error',
                'plugin' => $this->id,
                'name' => 'some_value_error',
                'text' => _w('My plugin’s validation message.')
            ];
        }
    }

    return [
        'errors' => $errors,
    ];
}