promo_before_save

Triggered before saving a promo’s properties to the database. Allows termination of the saving process by returning information about an error.

Shop-Script

Input (passed by reference)

$params['promo_id'] int Promo ID.
$params['is_new'] bool Whether a new promo is being saved which did not exist before.
$params['promo_data'] array Promo’s properties. Can be modified by a plugin.
$params['storefronts_data'] array List of storefronts selected in a promo’s properties. Array values are the sort values of the promo, which is being saved, for each of the storefronts, 0 meaning undefined sort order, i.e. the promo should be displayed at the end of the list. Can be modified by a plugin.
$params['delete_rule_ids'] array IDs of promo tools which a user has selected to delete from a promo. Can be modified by a plugin.
$params['rules'] array Promo tools’ properties. Can be modified by a plugin.
$params['edited_rules'] array Promo tools modified by a user.
$params['new_rules'] array Promo tools added by a user.
$params['old_rules'] array Promo tools not modified by a user.
… your plugin code …

Output

%plugin_id%]['errors']
Shop-Script

Plugin code example

PHP

public function promoBeforeSave($params)
{
    $edited_promo_rules = ifset($params, 'edited_rules', []);

    foreach ($edited_promo_rules as $rule_id => $rule) {
        if ($rule['rule_type'] == 'banner') {
            $banners = ifset($rule, 'rule_params', 'banners', []);

            foreach ($banners as $banner) {
                if (!preg_match('~\.png$~i', ifset($banner, 'image_filename', ''))) {
                    $invalid_banner_id = $rule_id;
                    break 2;
                }
            }
        }
    }

    if (!empty($invalid_banner_id)) {
        return [
            'errors' => [
                [
                    'id' => 'rule_error',
                    'rule' => sprintf('rules[%d]', $invalid_banner_id),
                    'rule_data' => [
                        'error_code' => 'invalid_banner_file'
                    ],
                    'text' => _wp('Upload a banner image in the PNG format!'),
                ],
            ],
        ];
    }
}