front page Forums Community lottery

Viewing 15 posts - 16 through 30 (of 33 total)
  • Author
  • #5313


    If you want to set the maximum number of entries per user, you can do this by editing the lottery and changing the Participate settings.

    If you want to set a global max for total number of entries for the lottery then you could add the following code to your themes functions.php file:

    add_filter( 'mycred_lotto_after_play', 'limit_max_lottery_entries', 10, 2 );
    function limit_max_lottery_entries( $reply, $lottery ) {
        // If the entry failed, leave now
        if ( $reply['status'] != 'done' ) return $reply;
        // Count lottery entries
        $count = $lottery->get_total_entries();
        // Limit
        $limit = 100;
        // If we are over the limit, close the lottery preventing more entries.
        if ( $count >= $limit ) {
            mycred_lottery_do_close( $lottery->id );
        // Always return something or we brake everything
        return $reply;

    This code will fire after a user successfully creates a new lottery entry. Once the entry is saved and charged, the script will count the total number of entries and if it exceeds or reaches 100 (you can change this to any number) it will close the lottery.


    So this would set a limit for every lottery that is run right? What if you need to set a different limit for each one. So 1 lottery might allow 100 entries but another might allow 200 entries. Could you possibly show us a code that would allow us to enter a max entry for each lottery we run?

    Also, does it let you select how many points to award on a draw? I couldn’t see this in the screen shots.

    Thanks for all your hard work



    You set the amount each drawn winner gets when you setup a lottery. You set the number of users you want to draw and save. Once saved you will see under “Winnings” (in the same metabox) a field for each drawn winner. So if you select to draw 3 winners, you will see 3 amount fields where you can set the amount of points each winner gets. The first row is for the first drawn winner, the second for the second drawn winner and so forth.

    In order to enforce a limit on a individual lottery basis we would need to save this limit somehow before we run the above code and then set the max based on this value.

    The lottery ID is a post ID and you can save custom meta fields for lotteries just like any other post type. You just replace $limit with the value you saved.


    Wow, thank you so much for the fast reply. This sounds great and really some fantastic work you are doing. I will just need to figure out a way to code in the ability to set max entries for each lottery. So when the clients I am building the site for creates the lottery they need a field for max entries. Once I had that I could put php code into my functions php. Just like you did, only it would pull the entered value as the $limit. I am thinking on the right lines here right? So the question then becomes, is there a way to code in a text field for them to enter a max number of entries when creating the lottery?


    I am on my way home but I would suggest you have a look in the mycred-lottery-core.php file which handles all meta boxes in the admin area. All meta boxes comes with their own custom action hooks that you can use to insert custom details. The question you’ll face is where to put in the setting.

    Either under Winnings or maybe under the game setup. Do a search in this file for “do_action” and you will find the hooks pretty quick.

    Once you have inserted your field it comes down to a simple save_post hook where you save the detail using update_post_meta.

    Once you have that saved just use get_post_meta to get the saved value and adjust the code to make sure if no limit is set, you either enforce a default setting or just abort the process.


    great thanks for your help. I will take a look into it


    Ok so I have put in a new text box, under the game set up area. So you can enter a ticket limit for each lottery. I did this by adding ‘tickets’ => 200 into the ‘game’ portion of the my_cred_default array. This in the mycred-lottery/classes/mycred-lottery.php file. Then I simply add another table area for the input box right under the No. of winners to draw area.

    I can now enter a number there and it saves just fine. i can leave the page and come back to edit and the number will be the same that I entered. So that portion is working fine.

    My problem now is I can not figure out how to call that data and close the drawing if the ticket limit is reached. I am trying to do this with do close function that already exists by just adding on or to the if statement. But I can not get the number that is entered when setting up the lottery to transfer. I have tried several ways and have been unsuccessful. This is my last attempt:

    update_post_meta( $lottery_id, ‘mycred_lotto_total_entries’, ‘mycred_lotto[game][tickets]’, $count );

    // Update next close timestamp
    $schedule = $lottery->get_schedule_prefs();
    $limit = $lottery->mycred_lotto[game][tickets];
    if ( $schedule[‘run’] != ‘once’ || $limit >= $count )
    update_post_meta( $lottery_id, ‘mycred_schedule_close’, $lottery->get_next_timestamp( ‘close’ ) );

    Any Help would be very much appreciated.
    Thank you



    Ok so is this changes in the lottery plugin files? If it is then remember that they will be deleted the next time you update the lottery plugin.

    This is how I would do it:


    That’s great! Thanks so much for the help. And I assume you are putting this into the theme’s function.php is that right?


    Always in your themes functions.php unless you are creating a custom plugin.


    Ok so this is not working. The box is there under Participation, but it is not saving. When I enter a number of tickets and then update lottery, the number I entered disappears. And the lottery, even though showing as running, is actually closed and will not allow entries, even though there has been no entries yet.


    Ok I see I made a mistake in the code which I have now updated.
    Make sure you remove any customizations you have already added yourself to make sure there are no conflicts.

    I would also recommend that you delete your lottery that you have issues with and try this code with a new lottery. I also assume you are using the latest version of the lottery add-on (1.1.1).


    ello, sorry to keep bugging you, but know I keep getting an unexpected $end for the end of this code. I have tried adding ?> to the end. I have tried adding another } to the end. And I have tried putting the { } into the if statements. And even adding the brackets along with elseif to the last function. All to no evail. Everything I do changes nothing. I can not see anything missing in the code, can you?


    Nevermind I got it. when I added the { I missed a closing one. Thanks again so much for your help. You are fantastic!


    Well still have the same issue with max number of tickets not being saved. I enter the number of tickets for the lotto and update lottery. The number I entered disappears. The lottery runs but never closes once the max tickets is reached. I even deleted the plug in and reinstalled it to make sure there were no changes to the original design.

Viewing 15 posts - 16 through 30 (of 33 total)

You must be logged in to reply to this topic.