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!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.