How to create Unique Coupon Codes

Although the discount field available in the RSForm!Pro Payment Package does not have by default a Validation Rule, this can be easily achieved through custom scripting. You can edit your Discount field and add something similar in the 'Coupon Code' area:

//<code>
$db = Joomla\CMS\Factory::getDbo();

// change this if you want to use a different discount from the list below
$uniqueDiscounts = ['DISCOUNT_ABCD'];
// change this to your Form ID
$formId = 1;
// change this to your field name
$fieldName = 'discount_field_name';

$discounts = [
    '10%|DISCOUNT_ABCD',
    '10%|DISCOUNT_1234',
    '10%|DISCOUNT_EFGH',
];

// get only the discount codes
$discountsCodes = [];
foreach($discounts as $discount)
{
    list($value, $code) = explode('|', $discount, 2);
    $discountsCodes[] = $code;
}

foreach ($uniqueDiscounts as $discountCode)
{
    $query = $db->getQuery(true);
    $query->clear()
        ->select("COUNT(*)")
        ->from('#__rsform_submission_values')
        ->where($db->qn('FormId').' = '.$db->q($formId)) // form id
        ->where($db->qn('FieldName').' = '.$db->q($fieldName)) // name of the field
        ->where($db->qn('FieldValue').' = '.$db->q($discountCode));
    $db->setQuery($query);

    $used = (int) $db->loadResult();

    // search in the discount codes
    if ($used && in_array($discountCode, $discountsCodes))
    {
        $key = array_search($discountCode, $discountsCodes);

        // remove from the actual discounts
        unset($discounts[$key]);
    }
}

return $discounts;
//</code>

Make sure to replace (1) with your form's Id and 'discount_field_name' with the actual name of your field.

 

Was this article helpful?

Yes No
Sorry about that