Advertisement
designbymerovingi

myCRED Woo Coupon - Log & Ref attr

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