coupon Query

front page Forums Community coupon Query

Viewing 15 posts - 1 through 15 (of 21 total)
  • Author
  • #9403

    Hi there,

    Could you please let me know the answer to a few questions I have regarding the Coupons addon / functionality of MyCred?

    1> Where are the Coupons stored? Are these in the Database table? If so, which one?

    2> Can we bulk add a list of Coupon codes, and pre define the amount of points each code is worth? As in, 100 codes, each worth 10 points each, bulk uploaded via a CSV file?

    3> If this functionality cannot be added, can you please let me know how I can add them via PHPMyAdmin, if this is indeed where they are stored (within the Database).

    Many thanks,


    Hi Thomas.

    1. Coupons are custom post types and are stored with all other post types in your wp_posts table. The coupons details are stored as a post meta which goes into your wp_postmeta table.

    2. There is no built in bulk upload / creation tool in myCRED for coupons.

    Theoretically should be able to import “Coupons” like you would import posts or pages but you would need to make sure the appropriate meta is also included.


    Concerning the coupons, do you think we can cleanly bulk-create coupons by executing a loop call ?


    Yeah of course. myCRED has a function that handles coupon creations called mycred_create_new_coupon.

    Example 1: Create 100 coupons worth 10 points each that can only be used once without any minimum or maximum balance requirement:

    $number = 100;
    $value = 10;
    for ( $i = 0; $i < $number; $i++ ) {
    	mycred_create_new_coupon( array( 'value' => $value ) );

    Example 2: Create 100 coupons worth 10 points each that expires on January 1st 2015 without any requirements:

    $number = 100;
    $value = 10;
    // YYYY MM DD
    $expires = '2015-01-01';
    for ( $i = 0; $i < $number; $i++ ) {
    	mycred_create_new_coupon( array(
    		'value'   => $value,
    		'expires' => $expires
    	) );

    Example 3: Create 100 coupons worth 10 points each that requires a user to have minimum 5 points in order to use:

    $number = 100;
    $value = 10;
    $min_balance = 5;
    for ( $i = 0; $i < $number; $i++ ) {
    	mycred_create_new_coupon( array(
    		'value'       => $value,
    		'min_balance' => $min_balance
    	) );

    Of course these will be unique coupons where the code will be generated for you so you can not create your own coupon codes using this function.

    If you want you can also create one coupon that can be used 100 times by setting the Global limit to 100.


    Hi there,

    With regards to the above codes, to create 100 coupons, etc, forgive my ignorance, but where are these placed?

    Also, I will be in touch to purchase the bulk licenses for the Lottery addon shortly – Thanks for the help!


    Ah forgive me, I assumed you had a place in mind where to use this code so I just provided examples for the insertion alone.

    The way these code snippets are used (and where) depends on how you want to create coupons. A simple example would be to create a custom shortcode that you use on a restricted page to bulk create coupons.

    I have written a custom shortcode as an example doing just this.
    I have uploaded the code here which is placed in your theme’s functions.php file.

    Once you pasted it in, create a custom “Private” page that only admins can access and insert the shortcode.


    You can then create any number of coupons along with viewing the codes right away after creation (if you prefer).


    Ah, fantastic! Thanks so much for this, it’s most helpful!

    Just so I can tinker, and make it friendly for my users to use (Students, aged 11-18), is it possible for it to generate an 8 digit numerical coupon at all, rather than the random letters / characters?


    Yeah of course. myCRED uses a custom function which checks to make sure each custom generated code is unique but you can customize this via some custom code.

    I use letters and numbers to minimize users trying to enter coupon codes at random. The downside of just using numbers is that you can loop though them and sooner or later you will find coupons.

    Here is an example which will generate a random number between 12345678 and 99999999 (to make it 8 characters long):

    add_filter( 'mycred_get_unique_coupon_code', 'mycred_custom_coupon_codes' );
    function mycred_custom_coupon_codes( $code ) {
    	global $wpdb;
    	do {
    		$id = rand( 12345678, 99999999 );
    		$query = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s;", $id, 'mycred_coupon' ) );
    	} while ( ! empty( $query ) );
    	return $id;

    the code goes into your theme’s functions.php file.


    Brilliant, thanks. The codes we wanted to use are as follows:

    3 Letters, followed by a 6 digit number.

    Example: DEH594827, DEH787654, DEH019283, DEH849506

    DEH = Code of the teacher issuing the coupon. Is this possible to create?

    Your support is brilliant, I must say!


    Most things are possible, but it brings up a question. Would this teacher code be unique for each teacher or same for all? I am thinking maybe we can add in an extra option for the shortcode for you to set the prefix of each code so you generate first codes for one teacher then for another?


    It would be unique to each teacher, so it would, as you say, probably benefit from being added as an extra option for the Shortcode, so each teacher can generate 1 set amount of bulk codes themselves, with their prefix (3 Letters), and then the 6 digit code itself.


    Ok. I have created a pastebin where I combine the previous code (the shortcode) with the filter to adjust how IDs are created. Please remove your previous code (both the shortcode and the filter) and use this code instead.

    The new shortcode has a extra field called “Teacher Prefix” which will be appended to the front of the id (which has been changed to be 6 characters long).

    Please note that this prefix will always be uppercase.


    Gabriel, you are fantastic, it works perfectly, thank you!

    Do you know how I can (completely) clear all Coupons I currently have in the system, so I can begin again?


    Go to myCRED > Coupons and move all existing coupons you see into the trash then empty the trash.


    I currently have 2000+ as I was playing with a previous code which added several hundred at a time. Whoops!

Viewing 15 posts - 1 through 15 (of 21 total)

You must be logged in to reply to this topic.