Advertisement
designbymerovingi

myCRED Points to MarketPress Coupon

Jan 29th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.54 KB | None | 0 0
  1. /**
  2.  * Convert myCRED Points into MarketPress Coupon
  3.  * This shortcode will present a form where users can
  4.  * convert points into MarketPress coupons.
  5.  * @version 1.0
  6.  */
  7. add_shortcode( 'mycred_to_mpress_coupon', 'mycred_pro_render_points_to_marketpress_coupon' );
  8. function mycred_pro_render_points_to_marketpress_coupon( $atts, $content = NULL ) {
  9.     // Users must be logged in
  10.     if ( ! is_user_logged_in() )
  11.         return 'You must be logged in to generate store coupons.';
  12.  
  13.     // myCRED must be enabled
  14.     if ( ! function_exists( 'mycred_get_settings' ) )
  15.         return 'myCRED must be enabled to use this shortcode';
  16.  
  17.     // Load myCRED
  18.     $mycred = mycred_get_settings();
  19.  
  20.     // Prep
  21.     $error = $code = false;
  22.     $output = '';
  23.     $user_id = get_current_user_id();
  24.  
  25.     // No need to show this for excluded users
  26.     if ( $mycred->exclude_user( $user_id ) ) return;
  27.  
  28.     $balance = $mycred->get_users_balance( $user_id );
  29.  
  30.     // Form submission
  31.     if ( isset( $_POST['mycred_to_mark'] ) && wp_verify_nonce( $_POST['mycred_to_mark']['token'], 'points-to-market-coupon' ) ) {
  32.  
  33.         // Make sure amounts are always positive
  34.         $amount = abs( $_POST['mycred_to_mark']['amount'] );
  35.        
  36.         // Assumes 1 point = 1 currency. You would need to apply an
  37.         // exchange rate here before we continue on.
  38.  
  39.         // Make sure amount is not zero
  40.         if ( $amount == $mycred->zero() )
  41.             $error = 'Amount can not be zero';
  42.  
  43.         // Make sure user has enough points
  44.         if ( $amount > $balance )
  45.             $error = 'Insufficient Funds. Please try a lower amount';
  46.  
  47.         // If no errors
  48.         if ( $error === false ) {
  49.  
  50.             // In this example coupons are valid for 30 days
  51.             // You can change this to a specific date or change
  52.             // the number of days. We need a unix timestamp.
  53.             $expires = strtotime( '+ 30 days' );
  54.  
  55.             // In this exmaple we will want users to be able to use
  56.             // the generated coupon right away but you can change thi
  57.             // to any unix timestamp as long as it is befor the expire date.
  58.             $start = time();
  59.  
  60.             // Create MarketPress Coupon
  61.             $code = wp_generate_password( 12, false, false );
  62.             $code = strtoupper( $code );
  63.             $coupon = array(
  64.                 'discount_type' => 'amt',
  65.                 'discount'      => $amount,
  66.                 'start'         => $start,
  67.                 'end'           => $expires,
  68.                 'uses'          => 1
  69.             );
  70.            
  71.             // Save
  72.             $coupons = get_option( 'mp_coupons', array() );
  73.             $coupons[ $code ] = $coupon;
  74.             update_option( 'mp_coupons', $coupons );
  75.  
  76.             // Deduct points from user
  77.             $mycred->add_creds(
  78.                 'points_to_coupon',
  79.                 $user_id,
  80.                 0-$amount,
  81.                 '%plural% conversion into store coupon',
  82.                 $new_coupon_id,
  83.                 array( 'code' => $code )
  84.             );
  85.  
  86.             $balance = $balance-$amount;
  87.             $balance = $mycred->number( $balance );
  88.         }
  89.  
  90.     }
  91.  
  92.     // Show users current balance
  93.     $output .= '
  94. <p>Your current balance is: ' . $mycred->format_creds( $balance ) . '</p>';
  95.  
  96.     // Error
  97.     if ( $error !== false )
  98.         $output .= '<p style="color:red;">' . $error . '</p>';
  99.  
  100.     // Success
  101.     elseif ( $code !== false )
  102.         $output .= '<p>Your coupon code is: <strong>' . $code . '</strong></p>';
  103.  
  104.     // The form for those who have points
  105.     if ( $balance > $mycred->zero() )
  106.         $output .= '
  107. <form action="" method="post">
  108.     <input type="hidden" name="mycred_to_mark[token]" value="' . wp_create_nonce( 'points-to-market-coupon' ) . '" />
  109.     <label>Amount</label>
  110.     <input type="text" size="5" name="mycred_to_mark[amount]" value="" />
  111.     <input type="submit" name="submit" value="Create Coupon" />
  112. </form>';
  113.  
  114.     // Not enough points
  115.     else
  116.         $output .= '<p>Not enough points to create coupons.</p>';
  117.  
  118.     return $output;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement