Advertisement
designbymerovingi

1000 Points to 10% Woo Coupon

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