Advertisement
designbymerovingi

Teacher Bulk Coupon Creation

Sep 12th, 2014
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.94 KB | None | 0 0
  1. add_shortcode( 'mycred_create_bulk_coupons', 'mycred_render_bulk_coupon_creation' );
  2. function mycred_render_bulk_coupon_creation() {
  3.  
  4.     if ( ! current_user_can( 'manage_options' ) ) return 'You do not create coupons.';
  5.     if ( ! function_exists( 'mycred_create_new_coupon' ) ) return 'The Coupon add-on is not enabled!';
  6.  
  7.     if ( isset( $_POST['mycred_coupon_bulk'] ) && isset( $_POST['cc-token'] ) && wp_verify_nonce( $_POST['cc-token'], 'mycred-coupon-bulk-create' ) ) {
  8.  
  9.         $number = $_POST['mycred_coupon_bulk']['number'];
  10.         $value = $_POST['mycred_coupon_bulk']['value'];
  11.         $expire = $_POST['mycred_coupon_bulk']['expire'];
  12.         $global_limit = $_POST['mycred_coupon_bulk']['global'];
  13.         $user_limit = $_POST['mycred_coupon_bulk']['user'];
  14.         $min_balance = $_POST['mycred_coupon_bulk']['min'];
  15.         $max_balance = $_POST['mycred_coupon_bulk']['max'];
  16.  
  17.         $views = array();
  18.         $ids = array();
  19.  
  20.         add_filter( 'mycred_get_unique_coupon_code', 'mycred_custom_coupon_codes' );
  21.  
  22.         for ( $i = 0; $i < $number; $i++ ) {
  23.             $post_id = mycred_create_new_coupon( array(
  24.                 'value'       => $value,
  25.                 'global_max'  => $global_limit,
  26.                 'user_max'    => $user_limit,
  27.                 'min_balance' => $min_balance,
  28.                 'max_balance' => $max_balance,
  29.                 'expires'     => $expire
  30.             ) );
  31.  
  32.             $ids[] = $post_id;
  33.             if ( isset( $_POST['mycred_coupon_bulk']['view'] ) )
  34.                 $views[] = get_the_title( $post_id );
  35.  
  36.         }
  37.  
  38.     }
  39.  
  40.     ob_start();
  41. ?>
  42. <h3>Bulk Coupon Creator</h3>
  43. <?php if ( ! empty( $ids ) ) : ?>
  44.     <h4><?php printf( '%d Coupons were successfully created.', count( $ids ) ); ?></h4>
  45.     <?php if ( ! empty( $views ) ) : ?>
  46.     <p>Your coupon codes are:</p>
  47.     <pre><?php echo implode( ' ', $views ); ?></pre>
  48.     <?php endif; ?>
  49. <?php else : ?>
  50.     <form method="post" action="">
  51.         <input type="hidden" name="cc-token" value="<?php echo wp_create_nonce( 'mycred-coupon-bulk-create' ); ?>" />
  52.         <p>
  53.             <label for="bulk-number">Teacher Prefix</label><br />
  54.             <input type="text" name="mycred_coupon_bulk[prefix]" id="bulk-number" value="" size="8" />
  55.         </p>
  56.         <p>
  57.             <label for="bulk-number">Number of Coupons to create</label><br />
  58.             <input type="text" name="mycred_coupon_bulk[number]" id="bulk-number" value="1" size="8" />
  59.         </p>
  60.         <p>
  61.             <label for="bulk-view"><input type="checkbox" name="mycred_coupon_bulk[view]" id="bulk-view" value="1" /> View Coupon codes when finished.</label>
  62.         </p>
  63.         <p>
  64.             <label for="bulk-value">Coupon Value</label><br />
  65.             <input type="text" name="mycred_coupon_bulk[value]" id="bulk-value" value="1" size="8" />
  66.         </p>
  67.         <p>
  68.             <label for="bulk-expire">Expiry Date</label><br />
  69.             <input type="text" name="mycred_coupon_bulk[expire]" id="bulk-expire" value="" size="20" placeholder="YYYY-MM-DD" />
  70.         </p>
  71.         <p>
  72.             <label for="bulk-global">Global Limit</label><br />
  73.             <input type="text" name="mycred_coupon_bulk[global]" id="bulk-global" value="1" size="8" />
  74.         </p>
  75.         <p>
  76.             <label for="bulk-user">User Limit</label><br />
  77.             <input type="text" name="mycred_coupon_bulk[user]" id="bulk-user" value="1" size="8" />
  78.         </p>
  79.         <p>
  80.             <label for="bulk-min">Minimum Balance Requirement</label><br />
  81.             <input type="text" name="mycred_coupon_bulk[min]" id="bulk-min" value="0" size="8" />
  82.         </p>
  83.         <p>
  84.             <label for="bulk-max">Maximum Balance Requirement</label><br />
  85.             <input type="text" name="mycred_coupon_bulk[max]" id="bulk-max" value="0" size="8" />
  86.         </p>
  87.         <p><input type="submit" class="button button-primary btn btn-primary btn-lg" value="Generate" /></p>
  88.     </form>
  89. <?php endif; ?>
  90. <?php
  91.  
  92.     $content = ob_get_contents();
  93.     ob_end_clean();
  94.     return $content;
  95.  
  96. }
  97.  
  98. function mycred_custom_coupon_codes( $code ) {
  99.     global $wpdb;
  100.  
  101.     $prefix = strtoupper( $_POST['mycred_coupon_bulk']['prefix'] );
  102.     do {
  103.  
  104.         $id = $prefix . rand( 123456, 999999 );
  105.         $query = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s;", $id, 'mycred_coupon' ) );
  106.  
  107.     } while ( ! empty( $query ) );
  108.  
  109.     return $id;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement