Hey guys,
I’m trying to make a coupons system by ACF PRO, but can’t figure out how to make random number for code…
I have repeater field for the coupons, and I want to make a sub-field for coupon code.
If I would be able to migrate a random code + other steady field it will be great
example:
steady input – ‘EXAMPLE’
reapter -> sub -> 761, 942, 329
Then it will become – EXAMPLE761, EXAMPLE942, EXAMPLE329
Thanks for helping 🙂
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!
Hey @keithlock,
Thanks for helping.
It’s seems to be a good solution, except that I don’t know how many coupons there’s gonna be, plus what if I make 3 coupons, then want to add another one? I should update the number input over and over again?
In addition to that, I can’t understand where I should put that piece of code? I need that in the moment that someone make a new row in the repeater input, it will get the “EXAMPLE” word and add to that some 3 random number.
There is some function/hook to run in the second that user create new row in reapter? so I can run a function that get the steady field word and add to that random number and update the sub-field?
You can place the code in your theme’s functions.php file or in a custom plugin. This link explains the differences. I like to use custom plugins, that way, if switching themes, my customizations remain. Using the Pluginception plugin (not necessarily supported by ACF, just a tool I like to suggest to simplify things) allows you to quickly create a plugin for this purpose.
I can’t fully understand your exact use case. Please explain it step by step with full details 😉 This thread might get you going otherwise.
I have website, and posts, in every post I have text field which called “codename” and repeater field which called “coupons” – it have 3 sub-fields – name, description, code
For example – I create post, and fill in “EXAMPLE” in the “codename”
Then I want to create coupons. what I want the system to do is –
Everytime I’ll click on “add row” I’ll get a new row in the repeater (default behavior), BUT I want the sub-field “code” to get value automatically “EXAMPLE-764” or “EXAMPLE-421” etc…
I should take automatically the field codename and add to it a 3 digits random number
To make random number – I know.
I just need to know:
1. how to run function when admin click “add row”
2. how to get the value in other field (before it published…)
3. how to update the subfield value in the add post page (and not in database, after the post was published, like “update_field” function does)
Hope it is more understandable now
Thanks for helping 🙂
Hmmm… this could be tricky.
First… have a peek through this thread here.
You will see that.. in order to pre-populate a repeater, the rows will have to exist ahead of time.
What if…
1) You had a ‘codename’ text field.
2) You had a ‘howmany’ number field (to decide how many codes to generate).
3) You had a textarea field to store your coupons…
… you could store them one per line (and just ‘append’ them when you add more), and then separate the data with a “pipe” | symbol to deal with later. You can use the PHP explode() function to split the data by the “pipe” symbol.
Example:
Codename: EXAMPLE
Howmany: 3
Generated Coupons:
Name1|Description1|Example122
Name2|Description2|Example123
Name3|Description4|Example999
Here’s a page that describes how to extract each line from a textarea field. Then, of course, use ‘explode’ to separate the data from each line.
Hope that helps. There may be a better way, it’s just not coming to me if so 🙂
@keithlock Hey
Can you please let me know how to run a function that update sub-field when I click on “add row” button?
Because “acf/load_value” not make it.. it just overwrite the exist rows, but new row are still empty
I give up for the word,
just want to run function that when I click on “add row” – I will get new row with empty fields, except one field that will be random 3 digits
Hey Everyone, I am here to find a site which can provide me with discounts and coupons as well as cashback on all kinds of sports products. As I am interested in sports as everybody does. For this I went through different sites then I found a site revglue.com/blog-detail/3-how-to-setup-a-responsive-uk-cashback-or-coupons-website-quickly-and-cheaply which is almost according to my interest and provides the users discounts on sport products as well as on different store products. Is there anyone else who has used or knows about this site? If yes, kindly share your views with me or do recommend me any similar site like which I mentioned above. I would be glad if someone helps me out in this regard. Thanks
The topic ‘Random code field for coupons system’ is closed to new replies.
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.