Advertisement
designbymerovingi

Custom myCRED to Woo Coupon Generator

Dec 16th, 2014
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.98 KB | None | 0 0
  1. /**
  2.  * Convert myCRED Points into WooCommerce Coupon
  3.  * @version 1.3
  4.  */
  5. add_shortcode( 'mycred_to_woo_coupon', 'mycred_pro_render_points_to_coupon' );
  6. function mycred_pro_render_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.         'amount'           => NULL,
  18.         'type'         => 'mycred_default',
  19.         'button_label' => 'Create Coupon',
  20.         'product_categories' => ''
  21.     ), $atts ) );
  22.  
  23.     // Load myCRED
  24.     $mycred = mycred( $type );
  25.  
  26.     // Prep
  27.     $original = $amount;
  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.         if ( $amount === NULL ) {
  42.             $original = NULL;
  43.             $amount = abs( $_POST['mycred_to_woo']['amount'] );
  44.         }
  45.  
  46.         // Exchange rate
  47.         $value = $mycred->number( $amount*$exchange );
  48.  
  49.         // Make sure amount is not zero
  50.         if ( $amount == $mycred->zero() )
  51.             $error = 'Amount can not be zero';
  52.  
  53.         // Make sure user has enough points
  54.         if ( $amount > $balance )
  55.             $error = 'Insufficient Funds. Please try a lower amount';
  56.  
  57.         // If no errors
  58.         if ( $error === false ) {
  59.  
  60.             // Deduct points from user
  61.             $charge = $mycred->add_creds(
  62.                 'points_to_coupon',
  63.                 $user_id,
  64.                 0-$amount,
  65.                 '%plural% conversion into store coupon: %post_title%',
  66.                 0,
  67.                 array( 'ref_type' => 'post' ),
  68.                 $type
  69.             );
  70.            
  71.             // If points were deducted create coupon
  72.             if ( $charge !== false ) {
  73.  
  74.                 // Create Woo Coupon
  75.                 $code = wp_generate_password( 12, false, false );
  76.                 $new_coupon_id = wp_insert_post( array(
  77.                     'post_title'   => $code,
  78.                     'post_content' => '',
  79.                     'post_status'  => 'publish',
  80.                     'post_author'  => 1,
  81.                     'post_type'    => 'shop_coupon'
  82.                 ) );
  83.  
  84.                 $balance = $balance-$amount;
  85.                 $balance = $mycred->number( $balance );
  86.  
  87.                 // Update Coupon details
  88.                 update_post_meta( $new_coupon_id, 'discount_type', 'fixed_cart' );
  89.                 update_post_meta( $new_coupon_id, 'coupon_amount', $value );
  90.                 update_post_meta( $new_coupon_id, 'individual_use', 'no' );
  91.                 update_post_meta( $new_coupon_id, 'product_ids', '' );
  92.                 update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
  93.  
  94.                 // Make sure you set usage_limit to 1 to prevent duplicate usage!!!
  95.                 update_post_meta( $new_coupon_id, 'usage_limit', 1 );
  96.                 update_post_meta( $new_coupon_id, 'usage_limit_per_user', 1 );
  97.                 update_post_meta( $new_coupon_id, 'limit_usage_to_x_items', '' );
  98.                 update_post_meta( $new_coupon_id, 'usage_count', '' );
  99.                 update_post_meta( $new_coupon_id, 'expiry_date', '' );
  100.                 update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
  101.                 update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
  102.  
  103.                 // If $product_categories is not an empty string then add the product categories limit
  104.                 // to the coupon. Otherwise this is ignored.
  105.                 if ( $product_categories != '' )
  106.                     update_post_meta( $new_coupon_id, 'product_categories', array( $product_categories ) );
  107.  
  108.                 update_post_meta( $new_coupon_id, 'exclude_product_categories', array() );
  109.                 update_post_meta( $new_coupon_id, 'exclude_sale_items', 'no' );
  110.                 update_post_meta( $new_coupon_id, 'minimum_amount', '' );
  111.                 update_post_meta( $new_coupon_id, 'customer_email', array() );
  112.  
  113.                 // Reset amount
  114.                 $amount = 0;
  115.  
  116.             }
  117.  
  118.             // Could not charge
  119.             else {
  120.  
  121.                 $error = 'You have reached your daily limit and can not create a coupon.';
  122.  
  123.             }
  124.  
  125.         }
  126.  
  127.     }
  128.  
  129.     // Show users current balance
  130.     $output .= '
  131. <p>Your current balance is: ' . $mycred->format_creds( $balance ) . '</p>';
  132.  
  133.     // Error
  134.     if ( $error !== false )
  135.         $output .= '<p style="color:red;">' . $error . '</p>';
  136.  
  137.     // Success
  138.     elseif ( $code !== false )
  139.         $output .= '<p>Your coupon code is: <strong>' . $code . '</strong></p>';
  140.  
  141.     // The form for those who have points
  142.     if ( $balance > $mycred->zero() ) {
  143.         $output .= '
  144. <form action="" method="post">
  145.     <input type="hidden" name="mycred_to_woo[token]" value="' . wp_create_nonce( 'points-to-woo-coupon' ) . '" />';
  146.    
  147.         // If shortcode is not set to use a specific amount
  148.         // we insert the amount field so one can be nominated
  149.         if ( $original === NULL )
  150.             $output .= '
  151.        <label>Amount</label>
  152.        <input type="text" size="5" name="mycred_to_woo[amount]" value="" />';
  153.  
  154.         $output .= '
  155.        <input type="submit" name="submit" value="' . $button_label . '" />
  156. </form>';
  157.  
  158.     }
  159.     // Not enough points
  160.     else
  161.         $output .= '<p>Not enough points to create coupons.</p>';
  162.  
  163.     return $output;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement