Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Convert myCRED Points into MarketPress Coupon
- * This shortcode will present a form where users can
- * convert points into MarketPress coupons.
- * @version 1.0
- */
- add_shortcode( 'mycred_to_mpress_coupon', 'mycred_pro_render_points_to_marketpress_coupon' );
- function mycred_pro_render_points_to_marketpress_coupon( $atts, $content = NULL ) {
- // Users must be logged in
- if ( ! is_user_logged_in() )
- return 'You must be logged in to generate store coupons.';
- // myCRED must be enabled
- if ( ! function_exists( 'mycred_get_settings' ) )
- return 'myCRED must be enabled to use this shortcode';
- // Load myCRED
- $mycred = mycred_get_settings();
- // Prep
- $error = $code = false;
- $output = '';
- $user_id = get_current_user_id();
- // No need to show this for excluded users
- if ( $mycred->exclude_user( $user_id ) ) return;
- $balance = $mycred->get_users_balance( $user_id );
- // Form submission
- if ( isset( $_POST['mycred_to_mark'] ) && wp_verify_nonce( $_POST['mycred_to_mark']['token'], 'points-to-market-coupon' ) ) {
- // Make sure amounts are always positive
- $amount = abs( $_POST['mycred_to_mark']['amount'] );
- // Assumes 1 point = 1 currency. You would need to apply an
- // exchange rate here before we continue on.
- // Make sure amount is not zero
- if ( $amount == $mycred->zero() )
- $error = 'Amount can not be zero';
- // Make sure user has enough points
- if ( $amount > $balance )
- $error = 'Insufficient Funds. Please try a lower amount';
- // If no errors
- if ( $error === false ) {
- // In this example coupons are valid for 30 days
- // You can change this to a specific date or change
- // the number of days. We need a unix timestamp.
- $expires = strtotime( '+ 30 days' );
- // In this exmaple we will want users to be able to use
- // the generated coupon right away but you can change thi
- // to any unix timestamp as long as it is befor the expire date.
- $start = time();
- // Create MarketPress Coupon
- $code = wp_generate_password( 12, false, false );
- $code = strtoupper( $code );
- $coupon = array(
- 'discount_type' => 'amt',
- 'discount' => $amount,
- 'start' => $start,
- 'end' => $expires,
- 'uses' => 1
- );
- // Save
- $coupons = get_option( 'mp_coupons', array() );
- $coupons[ $code ] = $coupon;
- update_option( 'mp_coupons', $coupons );
- // Deduct points from user
- $mycred->add_creds(
- 'points_to_coupon',
- $user_id,
- 0-$amount,
- '%plural% conversion into store coupon',
- $new_coupon_id,
- array( 'code' => $code )
- );
- $balance = $balance-$amount;
- $balance = $mycred->number( $balance );
- }
- }
- // Show users current balance
- $output .= '
- <p>Your current balance is: ' . $mycred->format_creds( $balance ) . '</p>';
- // Error
- if ( $error !== false )
- $output .= '<p style="color:red;">' . $error . '</p>';
- // Success
- elseif ( $code !== false )
- $output .= '<p>Your coupon code is: <strong>' . $code . '</strong></p>';
- // The form for those who have points
- if ( $balance > $mycred->zero() )
- $output .= '
- <form action="" method="post">
- <input type="hidden" name="mycred_to_mark[token]" value="' . wp_create_nonce( 'points-to-market-coupon' ) . '" />
- <label>Amount</label>
- <input type="text" size="5" name="mycred_to_mark[amount]" value="" />
- <input type="submit" name="submit" value="Create Coupon" />
- </form>';
- // Not enough points
- else
- $output .= '<p>Not enough points to create coupons.</p>';
- return $output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement