Advertisement
designbymerovingi

WooCommerce Coupon generator with limit

Dec 1st, 2014
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.43 KB | None | 0 0
  1. /**
  2.  * Convert myCRED Points into WooCommerce Coupon
  3.  * @version 1.2
  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_get_settings' ) )
  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.         // Make sure amounts are always positive
  38.         $amount = abs( $_POST['mycred_to_woo']['amount'] );
  39.        
  40.         // Exchange rate
  41.         $value = $mycred->number( $amount*$exchange );
  42.  
  43.         // Make sure amount is not zero
  44.         if ( $amount == $mycred->zero() )
  45.             $error = 'Amount can not be zero';
  46.  
  47.         // Make sure user has enough points
  48.         if ( $amount > $balance )
  49.             $error = 'Insufficient Funds. Please try a lower amount';
  50.  
  51.         // If no errors
  52.         if ( $error === false ) {
  53.  
  54.             // Deduct points from user
  55.             $charge = $mycred->add_creds(
  56.                 'points_to_coupon',
  57.                 $user_id,
  58.                 0-$amount,
  59.                 '%plural% conversion into store coupon: %post_title%',
  60.                 $new_coupon_id,
  61.                 array( 'ref_type' => 'post', 'code' => $code ),
  62.                 $type
  63.             );
  64.  
  65.             // If points were deducted create coupon
  66.             if ( $charge === true ) {
  67.  
  68.                 // Create Woo Coupon
  69.                 $code = wp_generate_password( 12, false, false );
  70.                 $new_coupon_id = wp_insert_post( array(
  71.                     'post_title'   => $code,
  72.                     'post_content' => '',
  73.                     'post_status'  => 'publish',
  74.                     'post_author'  => 1,
  75.                     'post_type'    => 'shop_coupon'
  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.             }
  103.  
  104.             // Could not charge
  105.             else {
  106.  
  107.                 $error = 'You have reached your daily limit and can not create a coupon.';
  108.  
  109.             }
  110.  
  111.         }
  112.  
  113.     }
  114.  
  115.     // Show users current balance
  116.     $output .= '
  117. <p>Your current balance is: ' . $mycred->format_creds( $balance ) . '</p>';
  118.  
  119.     // Error
  120.     if ( $error !== false )
  121.         $output .= '<p style="color:red;">' . $error . '</p>';
  122.  
  123.     // Success
  124.     elseif ( $code !== false )
  125.         $output .= '<p>Your coupon code is: <strong>' . $code . '</strong></p>';
  126.  
  127.     // The form for those who have points
  128.     if ( $balance > $mycred->zero() )
  129.         $output .= '
  130. <form action="" method="post">
  131.     <input type="hidden" name="mycred_to_woo[token]" value="' . wp_create_nonce( 'points-to-woo-coupon' ) . '" />
  132.     <label>Amount</label>
  133.     <input type="text" size="5" name="mycred_to_woo[amount]" value="" />
  134.     <input type="submit" name="submit" value="' . $button_label . '" />
  135. </form>';
  136.  
  137.     // Not enough points
  138.     else
  139.         $output .= '<p>Not enough points to create coupons.</p>';
  140.  
  141.     return $output;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement