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).
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.
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.
[mycred_create_bulk_coupons]
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.
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.
This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Strictly Necessary Cookies
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.