Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Convert myCRED Points into WooCommerce Coupon
- * @version 1.2
- */
- add_shortcode( 'mycred_to_woo_coupon', 'mycred_pro_render_points_to_coupon' );
- function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {
- // Users must be logged in
- if ( ! is_user_logged_in() )
- return 'You must be logged in to generate store coupons.';
- // myCRED must be enabled
- if ( ! function_exists( 'mycred_get_settings' ) )
- return 'myCRED must be enabled to use this shortcode';
- extract( shortcode_atts( array(
- 'exchange' => 1,
- 'type' => 'mycred_default',
- 'button_label' => 'Create Coupon',
- 'product_categories' => ''
- ), $atts ) );
- // Load myCRED
- $mycred = mycred( $type );
- // Prep
- $error = $code = false;
- $output = '';
- $user_id = get_current_user_id();
- // No need to show this for excluded users
- if ( $mycred->exclude_user( $user_id ) ) return;
- $balance = $mycred->get_users_balance( $user_id );
- // Form submission
- if ( isset( $_POST['mycred_to_woo'] ) && wp_verify_nonce( $_POST['mycred_to_woo']['token'], 'points-to-woo-coupon' ) ) {
- // Make sure amounts are always positive
- $amount = abs( $_POST['mycred_to_woo']['amount'] );
- // Exchange rate
- $value = $mycred->number( $amount*$exchange );
- // Make sure amount is not zero
- if ( $amount == $mycred->zero() )
- $error = 'Amount can not be zero';
- // Make sure user has enough points
- if ( $amount > $balance )
- $error = 'Insufficient Funds. Please try a lower amount';
- // If no errors
- if ( $error === false ) {
- // Deduct points from user
- $charge = $mycred->add_creds(
- 'points_to_coupon',
- $user_id,
- 0-$amount,
- '%plural% conversion into store coupon: %post_title%',
- $new_coupon_id,
- array( 'ref_type' => 'post', 'code' => $code ),
- $type
- );
- // If points were deducted create coupon
- if ( $charge === true ) {
- // Create Woo Coupon
- $code = wp_generate_password( 12, false, false );
- $new_coupon_id = wp_insert_post( array(
- 'post_title' => $code,
- 'post_content' => '',
- 'post_status' => 'publish',
- 'post_author' => 1,
- 'post_type' => 'shop_coupon'
- ) );
- $balance = $balance-$amount;
- $balance = $mycred->number( $balance );
- // Update Coupon details
- update_post_meta( $new_coupon_id, 'discount_type', 'fixed_cart' );
- update_post_meta( $new_coupon_id, 'coupon_amount', $value );
- update_post_meta( $new_coupon_id, 'individual_use', 'no' );
- update_post_meta( $new_coupon_id, 'product_ids', '' );
- update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
- // Make sure you set usage_limit to 1 to prevent duplicate usage!!!
- update_post_meta( $new_coupon_id, 'usage_limit', 1 );
- update_post_meta( $new_coupon_id, 'usage_limit_per_user', 1 );
- update_post_meta( $new_coupon_id, 'limit_usage_to_x_items', '' );
- update_post_meta( $new_coupon_id, 'usage_count', '' );
- update_post_meta( $new_coupon_id, 'expiry_date', '' );
- update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
- update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
- // If $product_categories is not an empty string then add the product categories limit
- // to the coupon. Otherwise this is ignored.
- if ( $product_categories != '' )
- update_post_meta( $new_coupon_id, 'product_categories', array( $product_categories ) );
- update_post_meta( $new_coupon_id, 'exclude_product_categories', array() );
- update_post_meta( $new_coupon_id, 'exclude_sale_items', 'no' );
- update_post_meta( $new_coupon_id, 'minimum_amount', '' );
- update_post_meta( $new_coupon_id, 'customer_email', array() );
- }
- // Could not charge
- else {
- $error = 'You have reached your daily limit and can not create a coupon.';
- }
- }
- }
- // Show users current balance
- $output .= '
- <p>Your current balance is: ' . $mycred->format_creds( $balance ) . '</p>';
- // Error
- if ( $error !== false )
- $output .= '<p style="color:red;">' . $error . '</p>';
- // Success
- elseif ( $code !== false )
- $output .= '<p>Your coupon code is: <strong>' . $code . '</strong></p>';
- // The form for those who have points
- if ( $balance > $mycred->zero() )
- $output .= '
- <form action="" method="post">
- <input type="hidden" name="mycred_to_woo[token]" value="' . wp_create_nonce( 'points-to-woo-coupon' ) . '" />
- <label>Amount</label>
- <input type="text" size="5" name="mycred_to_woo[amount]" value="" />
- <input type="submit" name="submit" value="' . $button_label . '" />
- </form>';
- // Not enough points
- else
- $output .= '<p>Not enough points to create coupons.</p>';
- return $output;
- }
- add_filter( 'mycred_add', 'restrict_points_to_once_per_day', 1, 3 );
- function restrict_points_to_once_per_day( $reply, $request, $mycred )
- {
- // If something else already declined this, respect it
- if ( $reply === false ) return $reply;
- // User ID
- $user_id = (int) $request['user_id'];
- // Today
- $today = date_i18n( 'Y-m-d' );
- // Get stored limit
- $limits = (array) get_user_meta( $user_id, 'daily_limits', true );
- // If no limits, or exisitng limit was for another date, reset
- if ( empty( $limits ) || ! isset( $limits[ $today ] ) ) {
- $limits[ $today ] = array();
- }
- // Check if ref exists
- if ( ! isset( $limits[ $today ][ $request['ref'] ] ) )
- $limits[ $today ][ $request['ref'] ] = 0;
- // Max 10 per reference
- if ( $limits[ $today ][ $request['ref'] ] >= 200 ) return false;
- // Increment
- $limits[ $today ][ $request['ref'] ] = $limits[ $today ][ $request['ref'] ]+1;
- // Save
- update_user_meta( $user_id, 'daily_limits', $limits );
- return $reply;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement