Advertisement
designbymerovingi

mycred-woo-coupon-with-amount

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