Created
September 12, 2014 10:42
-
-
Save gabrielmerovingi/921015b924da32f8b02a to your computer and use it in GitHub Desktop.
This custom WordPress shortcode can be used for bulk creation of myCRED Coupons. Make sure you use this shortcode in the admin area or on a protected page only available admins.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_shortcode( 'mycred_create_bulk_coupons', 'mycred_render_bulk_coupon_creation' ); | |
function mycred_render_bulk_coupon_creation() { | |
if ( ! current_user_can( 'manage_options' ) ) return 'You do not create coupons.'; | |
if ( ! function_exists( 'mycred_create_new_coupon' ) ) return 'The Coupon add-on is not enabled!'; | |
if ( isset( $_POST['mycred_coupon_bulk'] ) && isset( $_POST['cc-token'] ) && wp_verify_nonce( $_POST['cc-token'], 'mycred-coupon-bulk-create' ) ) { | |
$number = $_POST['mycred_coupon_bulk']['number']; | |
$value = $_POST['mycred_coupon_bulk']['value']; | |
$expire = $_POST['mycred_coupon_bulk']['expire']; | |
$global_limit = $_POST['mycred_coupon_bulk']['global']; | |
$user_limit = $_POST['mycred_coupon_bulk']['user']; | |
$min_balance = $_POST['mycred_coupon_bulk']['min']; | |
$max_balance = $_POST['mycred_coupon_bulk']['max']; | |
$views = array(); | |
$ids = array(); | |
for ( $i = 0; $i < $number; $i++ ) { | |
$post_id = mycred_create_new_coupon( array( | |
'value' => $value, | |
'global_max' => $global_limit, | |
'user_max' => $user_limit, | |
'min_balance' => $min_balance, | |
'max_balance' => $max_balance, | |
'expires' => $expire | |
) ); | |
$ids[] = $post_id; | |
if ( isset( $_POST['mycred_coupon_bulk']['view'] ) ) | |
$views[] = get_the_title( $post_id ); | |
} | |
} | |
ob_start(); | |
?> | |
<h3>Bulk Coupon Creator</h3> | |
<?php if ( ! empty( $ids ) ) : ?> | |
<h4><?php printf( '%d Coupons were successfully created.', count( $ids ) ); ?></h4> | |
<?php if ( ! empty( $views ) ) : ?> | |
<p>Your coupon codes are:</p> | |
<pre><?php echo implode( ' ', $views ); ?></pre> | |
<?php endif; ?> | |
<?php else : ?> | |
<form method="post" action=""> | |
<input type="hidden" name="cc-token" value="<?php echo wp_create_nonce( 'mycred-coupon-bulk-create' ); ?>" /> | |
<p> | |
<label for="bulk-number">Number of Coupons to create</label><br /> | |
<input type="text" name="mycred_coupon_bulk[number]" id="bulk-number" value="1" size="8" /> | |
</p> | |
<p> | |
<label for="bulk-view"><input type="checkbox" name="mycred_coupon_bulk[view]" id="bulk-view" value="1" /> View Coupon codes when finished.</label> | |
</p> | |
<p> | |
<label for="bulk-value">Coupon Value</label><br /> | |
<input type="text" name="mycred_coupon_bulk[value]" id="bulk-value" value="1" size="8" /> | |
</p> | |
<p> | |
<label for="bulk-expire">Expiry Date</label><br /> | |
<input type="text" name="mycred_coupon_bulk[expire]" id="bulk-expire" value="" size="20" placeholder="YYYY-MM-DD" /> | |
</p> | |
<p> | |
<label for="bulk-global">Global Limit</label><br /> | |
<input type="text" name="mycred_coupon_bulk[global]" id="bulk-global" value="1" size="8" /> | |
</p> | |
<p> | |
<label for="bulk-user">User Limit</label><br /> | |
<input type="text" name="mycred_coupon_bulk[user]" id="bulk-user" value="1" size="8" /> | |
</p> | |
<p> | |
<label for="bulk-min">Minimum Balance Requirement</label><br /> | |
<input type="text" name="mycred_coupon_bulk[min]" id="bulk-min" value="0" size="8" /> | |
</p> | |
<p> | |
<label for="bulk-max">Maximum Balance Requirement</label><br /> | |
<input type="text" name="mycred_coupon_bulk[max]" id="bulk-max" value="0" size="8" /> | |
</p> | |
<p><input type="submit" class="button button-primary btn btn-primary btn-lg" value="Generate" /></p> | |
</form> | |
<?php endif; ?> | |
<?php | |
$content = ob_get_contents(); | |
ob_end_clean(); | |
return $content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, can you give me a budget to implement a text field with Wysiwyg editor to personalize the message when I create a coupon? For example, a user redeems a coupon and the message I placed for that particular coupon appears below
Regards