Support

Account

Home Forums ACF PRO Random code field for coupons system Reply To: Random code field for coupons system

  • Hello @shonlevi ,

    If I understand correctly, perhaps you could have 3 fields…

    Field 1: Text box for value such as: EXAMPLE

    Field 2: Number… for how many coupons to generate

    Field 3: Repeater field for the coupon codes.

    Then, you could enter values in the first 2 boxes, and then use the update_field function to generate the codes..

    Here’s partial example code for updating a repeater:

    <?php
    $prefix = get_field('prefix', $post_id);
    $number_of_coupons = get_field('number_of_coupons', $post_id);
    $repeater_field_key = "field_12345678";
    $i = 1;
    while ($i <= $number_of_coupons) {
     //// use PHP 'rand' or 'shuffle' to generate some random numbers
     //// create a 'value' by joining the 'prefix' with a 'random number'
     //// put your logic in here to add values to the array
      $i++;
    }
    
    ///// here is how to add one value to the array
    $value = array(
      array(
        "coupon_code" => "Foo"
      )
    );
    update_field( $field_key, $value, $post_id );
    ?>

    Hope that gets you on the right path… clearly there is some work left to do and some testing, but that should get you going!