Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter( 'mycred_add', 'mycred_apply_transfer_fee', 20, 3 );
- function mycred_apply_transfer_fee( $reply, $request, $mycred ) {
- // If another filter declines this transfer ignore this request
- if ( $reply === false ) return $reply;
- // Make sure this is a transfer instance
- if ( $request['ref'] != 'transfer' ) return $reply;
- // In this example, we will deduct 1 point from the transfer
- // So if John transfers 10 points to Sara, we want to make sure
- // Sara only gets 10-1 = 9 points.
- // Make sure this is the transfer instance for Sara getting her points
- // We can diferenciate between John loosing his points and Sara getting hers
- // by making sure the amount is positive as Johns amount would be negative
- if ( $request['amount'] > 0 ) {
- // Deduct the fee. Note! DO NOT use the mycred_add or mycred_subtract functions
- // in this filter or you will create an infinite loop as both functions use this
- // filter. Instead we use the myCRED Settings object to take out the transfer fee.
- $mycred->update_users_balance( $request['user_id'], 0-1 );
- // Now Sara has been charged 1 point. We now need to add a log entry
- $mycred->add_to_log(
- 'transfer',
- $request['user_id'],
- 0-1,
- 'Transfer Fee'
- );
- // Now we just need to tell myCRED to add Sara the original 10 points John sent.
- // Since we have already deducted the "fee" it does not matter that she will receive
- // the full 10 points now.
- return true;
- }
- // Always return a reply or you brake everything!
- return $reply;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement