Advertisement
designbymerovingi

WooCommerce Coupon for Product Categories

Dec 5th, 2014
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.66 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.         'product_categories' => ''
  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.         $amount = abs( $_POST['mycred_to_woo']['amount'] );
  40.        
  41.         // Exchange rate
  42.         $value = $mycred->number( $amount*$exchange );
  43.  
  44.         // Make sure amount is not zero
  45.         if ( $amount == $mycred->zero() )
  46.             $error = 'Amount can not be zero';
  47.  
  48.         // Make sure user has enough points
  49.         if ( $amount > $balance )
  50.             $error = 'Insufficient Funds. Please try a lower amount';
  51.  
  52.         // If no errors
  53.         if ( $error === false ) {
  54.  
  55.             // Deduct points from user
  56.             $charge = $mycred->add_creds(
  57.                 'points_to_coupon',
  58.                 $user_id,
  59.                 0-$amount,
  60.                 '%plural% conversion into store coupon: %post_title%',
  61.                 $new_coupon_id,
  62.                 array( 'ref_type' => 'post', 'code' => $code ),
  63.                 $type
  64.             );
  65.            
  66.             // If points were deducted create coupon
  67.             if ( $charge === true ) {
  68.  
  69.                 // Create Woo Coupon
  70.                 $code = wp_generate_password( 12, false, false );
  71.                 $new_coupon_id = wp_insert_post( array(
  72.                     'post_title'   => $code,
  73.                     'post_content' => '',
  74.                     'post_status'  => 'publish',
  75.                     'post_author'  => 1,
  76.                     'post_type'    => 'shop_coupon'
  77.                 ) );
  78.  
  79.                 $balance = $balance-$amount;
  80.                 $balance = $mycred->number( $balance );
  81.  
  82.                 // Update Coupon details
  83.                 update_post_meta( $new_coupon_id, 'discount_type', 'fixed_cart' );
  84.                 update_post_meta( $new_coupon_id, 'coupon_amount', $value );
  85.                 update_post_meta( $new_coupon_id, 'individual_use', 'no' );
  86.                 update_post_meta( $new_coupon_id, 'product_ids', '' );
  87.                 update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
  88.  
  89.                 // Make sure you set usage_limit to 1 to prevent duplicate usage!!!
  90.                 update_post_meta( $new_coupon_id, 'usage_limit', 1 );
  91.                 update_post_meta( $new_coupon_id, 'usage_limit_per_user', 1 );
  92.                 update_post_meta( $new_coupon_id, 'limit_usage_to_x_items', '' );
  93.                 update_post_meta( $new_coupon_id, 'usage_count', '' );
  94.                 update_post_meta( $new_coupon_id, 'expiry_date', '' );
  95.                 update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
  96.                 update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
  97.  
  98.                 // If $product_categories is not an empty string then add the product categories limit
  99.                 // to the coupon. Otherwise this is ignored.
  100.                 if ( $product_categories != '' )
  101.                     update_post_meta( $new_coupon_id, 'product_categories', array( $product_categories ) );
  102.  
  103.                 update_post_meta( $new_coupon_id, 'exclude_product_categories', array() );
  104.                 update_post_meta( $new_coupon_id, 'exclude_sale_items', 'no' );
  105.                 update_post_meta( $new_coupon_id, 'minimum_amount', '' );
  106.                 update_post_meta( $new_coupon_id, 'customer_email', array() );
  107.  
  108.             }
  109.  
  110.             // Could not charge
  111.             else {
  112.  
  113.                 $error = 'You have reached your daily limit and can not create a coupon.';
  114.  
  115.             }
  116.  
  117.         }
  118.  
  119.     }
  120.  
  121.     // Show users current balance
  122.     $output .= '
  123. <p>Your current balance is: ' . $mycred->format_creds( $balance ) . '</p>';
  124.  
  125.     // Error
  126.     if ( $error !== false )
  127.         $output .= '<p style="color:red;">' . $error . '</p>';
  128.  
  129.     // Success
  130.     elseif ( $code !== false )
  131.         $output .= '<p>Your coupon code is: <strong>' . $code . '</strong></p>';
  132.  
  133.     // The form for those who have points
  134.     if ( $balance > $mycred->zero() )
  135.         $output .= '
  136. <form action="" method="post">
  137.     <input type="hidden" name="mycred_to_woo[token]" value="' . wp_create_nonce( 'points-to-woo-coupon' ) . '" />
  138.     <label>Amount</label>
  139.     <input type="text" size="5" name="mycred_to_woo[amount]" value="" />
  140.     <input type="submit" name="submit" value="' . $button_label . '" />
  141. </form>';
  142.  
  143.     // Not enough points
  144.     else
  145.         $output .= '<p>Not enough points to create coupons.</p>';
  146.  
  147.     return $output;
  148. }
  149.  
  150. add_filter( 'mycred_add', 'restrict_points_to_once_per_day', 1, 3 );
  151. function restrict_points_to_once_per_day( $reply, $request, $mycred )
  152. {
  153.     // If something else already declined this, respect it
  154.     if ( $reply === false ) return $reply;
  155.  
  156.     // User ID
  157.     $user_id = (int) $request['user_id'];
  158.  
  159.     // Today
  160.     $today = date_i18n( 'Y-m-d' );
  161.  
  162.     // Get stored limit
  163.     $limits = (array) get_user_meta( $user_id, 'daily_limits', true );
  164.    
  165.     // If no limits, or exisitng limit was for another date, reset
  166.     if ( empty( $limits ) || ! isset( $limits[ $today ] ) ) {
  167.         $limits[ $today ] = array();
  168.     }
  169.    
  170.     // Check if ref exists
  171.     if ( ! isset( $limits[ $today ][ $request['ref'] ] ) )
  172.         $limits[ $today ][ $request['ref'] ] = 0;
  173.    
  174.     // Max 10 per reference
  175.     if ( $limits[ $today ][ $request['ref'] ] >= 200 ) return false;
  176.  
  177.     // Increment   
  178.     $limits[ $today ][ $request['ref'] ] = $limits[ $today ][ $request['ref'] ]+1;
  179.    
  180.     // Save
  181.     update_user_meta( $user_id, 'daily_limits', $limits );
  182.    
  183.     return $reply;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement