Advertisement
designbymerovingi

WP Coupon with Email

Dec 16th, 2015
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.42 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Convert myCRED Points into WooCommerce Coupon
  5.  * Requires myCRED 1.4 or higher!
  6.  * @version 1.2.2
  7.  */
  8. add_shortcode( 'mycred_to_woo_coupon', 'mycred_pro_render_points_to_coupon' );
  9. function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {
  10.     // Users must be logged in
  11.     if ( ! is_user_logged_in() )
  12.         return 'You must be logged in to generate store coupons.';
  13.  
  14.     // myCRED must be enabled
  15.     if ( ! function_exists( 'mycred' ) )
  16.         return 'myCRED must be enabled to use this shortcode';
  17.  
  18.     extract( shortcode_atts( array(
  19.         'exchange'     => 1,
  20.         'type'         => 'mycred_default',
  21.         'button_label' => 'Create Coupon'
  22.     ), $atts ) );
  23.  
  24.     // Load myCRED
  25.     $mycred = mycred( $type );
  26.  
  27.     // Prep
  28.     $error = $code = false;
  29.     $output = '';
  30.     $user_id = get_current_user_id();
  31.  
  32.     // No need to show this for excluded users
  33.     if ( $mycred->exclude_user( $user_id ) ) return;
  34.  
  35.     $balance = $mycred->get_users_balance( $user_id );
  36.  
  37.     // Form submission
  38.     if ( isset( $_POST['mycred_to_woo'] ) && wp_verify_nonce( $_POST['mycred_to_woo']['token'], 'points-to-woo-coupon' ) ) {
  39.  
  40.         // Make sure amounts are always positive
  41.         $amount = abs( $_POST['mycred_to_woo']['amount'] );
  42.        
  43.         // Exchange rate
  44.         $value = $mycred->number( $amount*$exchange );
  45.  
  46.         // Make sure amount is not zero
  47.         if ( $amount == $mycred->zero() )
  48.             $error = 'Amount can not be zero';
  49.  
  50.         // Make sure user has enough points
  51.         if ( $amount > $balance )
  52.             $error = 'Insufficient Funds. Please try a lower amount';
  53.  
  54.         // If no errors
  55.         if ( $error === false ) {
  56.  
  57.             // Create Woo Coupon
  58.             $code = strtolower( wp_generate_password( 12, false, false ) );
  59.             $new_coupon_id = wp_insert_post( array(
  60.                 'post_title'   => $code,
  61.                 'post_content' => '',
  62.                 'post_status'  => 'publish',
  63.                 'post_author'  => 1,
  64.                 'post_type'    => 'shop_coupon'
  65.             ) );
  66.  
  67.             // Deduct points from user
  68.             $mycred->add_creds(
  69.                 'points_to_coupon',
  70.                 $user_id,
  71.                 0-$amount,
  72.                 '%plural% conversion into store coupon: %post_title%',
  73.                 $new_coupon_id,
  74.                 array( 'ref_type' => 'post', 'code' => $code ),
  75.                 $type
  76.             );
  77.  
  78.             $balance = $balance-$amount;
  79.             $balance = $mycred->number( $balance );
  80.  
  81.             // Update Coupon details
  82.             update_post_meta( $new_coupon_id, 'discount_type', 'fixed_cart' );
  83.             update_post_meta( $new_coupon_id, 'coupon_amount', $value );
  84.             update_post_meta( $new_coupon_id, 'individual_use', 'no' );
  85.             update_post_meta( $new_coupon_id, 'product_ids', '' );
  86.             update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
  87.  
  88.             // Make sure you set usage_limit to 1 to prevent duplicate usage!!!
  89.             update_post_meta( $new_coupon_id, 'usage_limit', 1 );
  90.             update_post_meta( $new_coupon_id, 'usage_limit_per_user', 1 );
  91.             update_post_meta( $new_coupon_id, 'limit_usage_to_x_items', '' );
  92.             update_post_meta( $new_coupon_id, 'usage_count', '' );
  93.             update_post_meta( $new_coupon_id, 'expiry_date', '' );
  94.             update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
  95.             update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
  96.             update_post_meta( $new_coupon_id, 'product_categories', array() );
  97.             update_post_meta( $new_coupon_id, 'exclude_product_categories', array() );
  98.             update_post_meta( $new_coupon_id, 'exclude_sale_items', 'no' );
  99.             update_post_meta( $new_coupon_id, 'minimum_amount', '' );
  100.             update_post_meta( $new_coupon_id, 'customer_email', array() );
  101.  
  102.             // Send coupon as an email just in case
  103.             $user = wp_get_current_user();
  104.             wp_mail( $user->user_email, 'Your Coupon Code', 'This is your new store coupon code: ' . $code );
  105.  
  106.         }
  107.  
  108.     }
  109.  
  110.     // Show users current balance
  111.     $output .= '
  112. <p>Your current balance is: ' . $mycred->format_creds( $balance ) . '</p>';
  113.  
  114.     // Error
  115.     if ( $error !== false )
  116.         $output .= '<p style="color:red;">' . $error . '</p>';
  117.  
  118.     // Success
  119.     elseif ( $code !== false )
  120.         $output .= '<p>Your coupon code is: <strong>' . $code . '</strong></p>';
  121.  
  122.     // The form for those who have points
  123.     if ( $balance > $mycred->zero() )
  124.         $output .= '
  125. <form action="" method="post">
  126.     <input type="hidden" name="mycred_to_woo[token]" value="' . wp_create_nonce( 'points-to-woo-coupon' ) . '" />
  127.     <label>Amount</label>
  128.     <input type="text" size="5" name="mycred_to_woo[amount]" value="" />
  129.     <input type="submit" name="submit" value="' . $button_label . '" />
  130. </form>';
  131.  
  132.     // Not enough points
  133.     else
  134.         $output .= '<p>Not enough points to create coupons.</p>';
  135.  
  136.     return $output;
  137. }
  138.  
  139. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement