Advertisement
designbymerovingi

myCRED Transfer Fee example

Nov 26th, 2013
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.53 KB | None | 0 0
  1. add_filter( 'mycred_add', 'mycred_apply_transfer_fee', 20, 3 );
  2. function mycred_apply_transfer_fee( $reply, $request, $mycred ) {
  3.     // If another filter declines this transfer ignore this request
  4.     if ( $reply === false ) return $reply;
  5.    
  6.     // Make sure this is a transfer instance
  7.     if ( $request['ref'] != 'transfer' ) return $reply;
  8.    
  9.     // In this example, we will deduct 1 point from the transfer
  10.     // So if John transfers 10 points to Sara, we want to make sure
  11.     // Sara only gets 10-1 = 9 points.
  12.    
  13.     // Make sure this is the transfer instance for Sara getting her points
  14.     // We can diferenciate between John loosing his points and Sara getting hers
  15.     // by making sure the amount is positive as Johns amount would be negative
  16.     if ( $request['amount'] > 0 ) {
  17.  
  18.         // Deduct the fee. Note! DO NOT use the mycred_add or mycred_subtract functions
  19.         // in this filter or you will create an infinite loop as both functions use this
  20.         // filter. Instead we use the myCRED Settings object to take out the transfer fee.
  21.         $mycred->update_users_balance( $request['user_id'], 0-1 );
  22.        
  23.         // Now Sara has been charged 1 point. We now need to add a log entry
  24.         $mycred->add_to_log(
  25.             'transfer',
  26.             $request['user_id'],
  27.             0-1,
  28.             'Transfer Fee'
  29.         );
  30.        
  31.         // Now we just need to tell myCRED to add Sara the original 10 points John sent.
  32.         // Since we have already deducted the "fee" it does not matter that she will receive
  33.         // the full 10 points now.
  34.         return true;
  35.     }
  36.    
  37.     // Always return a reply or you brake everything!
  38.     return $reply;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement