Italian (Italy)
Knowledge Base  >  Vik Restaurants  >  Hooks  >  System  >  Generate Serial Code

apply_filters_ref_array( 'vikrestaurants_generate_serial_code', bool $status, string &$code, array &$map, int $length, mixed $scope )

Fires while generating a serial code.


Description

A serial code is a random alphanumeric string that can be used for different purposes. Just to be clear, the Order Key column of the reservations and orders is a serial code. This hook can be used to change the way the system generates a serial code.

It is possible to edit the code or simply to alter the map of allowed tokens. In case the serial code didn't reach the specified length, the remaining characters will be generated according to the default algorithm.

By default the method generates a serial code containing only uppercase characters and digits.


Parameters

$status

(bool)  True on success, false otherwise.

&$code

(string)  The current serial code. Since the argument is passed by reference, it is possible to manipulate the serial code.

&$map

(array)  Either a linear or a multi-dimensional array containing the allowed tokens.

$length

(int)  The requested length of the serial code.

$scope

(string|null)  The purpose of the serial code (the entity that requested the code generation).
Here's a list of supported scopes.

  • coupon - the default random code used for the creation of a coupon;
  • order-confkey - the confirmation key used for take-away orders;
  • order-sid - the order key used for take-away orders;
  • reservation-confkey - the confirmation key used for restaurant reservations;
  • reservation-sid - the order key used for restaurant reservations;
  • review-confkey - the confirmation key used to approve new reviews.

The scope will be null only in case a third-party plugin calls the VikRestaurants::generateSerialCode($length, $scope, $map) method by omitting the $scope argument.


Example

The following example alters the default generation of a coupon code. Any generated coupon will start with "CPN" prefix and will contain only uppercase letters.

/**
 * This event can be used to change the way the system generates a serial code.
 * It is possible to edit the code or simply to alter the map of allowed tokens.
 * In case the serial code didn't reach the specified length, the remaining
 * characters will be generated according to the default algorhytm.
 *
 * @param  boolean  $status  True on success, false otherwise.
 * @param  string   &$code   The serial code.
 * @param  array    &$map    A lookup of allowed tokens.
 * @param  integer  $length  The length of the serial code.
 * @param  mixed    $scope   The purpose of the code.
 */
add_filter('vikrestaurants_generate_serial_code', function($status, &$code, &$map, $length, $scope)
{
    if ($scope == 'coupon')
    {
        // always start with "CPN"
        $code = 'CPN';
        // use only letters
        $map = array('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
    }

    return $status;
}, 10, 5);

Changelog

Version Description
1.0 Introduced.
Ultimo aggiornamento: 2021-01-25
Utile?