public function onGenerateSerialCode( string &$code, array &$map, int $length, mixed $scope ) : void

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

&$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.

Return Value

None.


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   string  &$code   The serial code.
 * @param   array   &$map    A lookup of allowed tokens.
 * @param   int     $length  The length of the serial code.
 * @param   mixed   $scope   The purpose of the code.
 *
 * @return  void
 */
public function onGenerateSerialCode(&$code, &$map, $length, $scope)
{
    if ($scope == 'coupon') {
        // always start with "CPN"
        $code = 'CPN';
        // use only letters
        $map = ['ABCDEFGHIJKLMNOPQRSTUVWXYZ'];
    }
}

Changelog

Version Description
1.8 Introduced.
Last Update: 2023-12-29 14:15
Helpful?