Advertisement
designbymerovingi

Award all users on WooCommerce purchase

Dec 4th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.94 KB | None | 0 0
  1. add_action( 'woocommerce_payment_complete', 'mycred_pro_give_points_to_all_woo', 10, 3 );
  2. function mycred_pro_give_points_to_all_woo( $order_id ) {
  3.  
  4.     // Make sure myCRED is installed
  5.     if ( ! function_exists( 'mycred' ) ) return;
  6.  
  7.     global $wpdb;
  8.  
  9.     // Load myCRED
  10.     $mycred = mycred();
  11.  
  12.     // Get all users that have a balance
  13.     $all_users = $wpdb->get_col( $wpdb->prepare( "
  14.         SELECT users.ID  
  15.         FROM {$wpdb->users} users
  16.         INNER JOIN {$wpdb->usermeta} balance
  17.             ON ( users.ID = balance.user_id )
  18.         WHERE balance.meta_key = %s
  19.         AND balance.meta_value != '';", $ctype ) );
  20.  
  21.     // Loop
  22.     if ( $all_users ) {
  23.         foreach ( $all_users as $user_id ) {
  24.  
  25.             // Make sure user is not excluded
  26.             if ( $mycred->exclude_user( $user_id ) ) continue;
  27.  
  28.             // Award
  29.             $mycred->add_creds(
  30.                 'product_purchase',
  31.                 $user_id,
  32.                 10,
  33.                 '%plural% for store purchase',
  34.                 $order_id,
  35.                 array( 'ref_type' => 'post' )
  36.             );
  37.  
  38.         }
  39.     }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement