• Resolved marotiem

    (@marotiem)


    I have been using mycred link to add and deduct points. The default mode is that users’ balance can go negative, so the transaction does not stop even when a user does not have enough points. So I used this code snippet provided by mycred to prevent negative balance: https://gist.github.com/gabrielmerovingi/8c03987b961d85c1ddbee8654b520ed0

    Unfortunately, the code does not work. It prevents any kind of deductions, even when the user has enough points.

    The page I need help with: [log in to see the link]

Viewing 14 replies - 1 through 14 (of 14 total)
  • A.Tariq

    (@arsalantariq)

    Hi @marotiem,

    Thank you for contacting us, With this code no negative logs will be created and user’s balance will never become negative. Try using the below code snippet and let us know if it works for you.

    add_filter( 'mycred_add', 'mycred_pro_no_negative_balance', 999, 2 ); function mycred_pro_no_negative_balance( $reply, $request ) {
    $mycred = mycred( $request['type'] ); if ( isset( $request['user_id'] ) ) { if ( ! $mycred->exclude_user( $request['user_id'] ) ) { if ( $mycred->get_users_balance( $request['user_id'], $request['type'] ) <= 0 ) return false; } } return $reply;}

    Thread Starter marotiem

    (@marotiem)

    Hi Tariq! Thank you for the code. It does deductions, but unfortunately it allows negative balance. What I need, is a code that does NOT allow negative balance, so when the user would like to acquire something that’s worth more points than the amount he has, the points will not be deducted, and an error message comes up. So deduction is ok, as long as the balance does not go below zero. Also, since I am using mycred link, I want the link not to redirect if balance is not enough.

    A.Tariq

    (@arsalantariq)

    @marotiem,

    Please use the following code snippet in your theme’s functions.php file:

    add_filter('mycred_add', 'mycred_pro_no_negative_balance', 999, 2); function mycred_pro_no_negative_balance( $reply, $request ) {
    $mycred = mycred( $request['type'] ); // Ignore if allready declined if ( $reply === false ) return $reply; $balance = $mycred->get_users_balance( $request['user_id'], $request['type'] ); $request_new = $balance ; $request_old = $balance - $request['amount']; if ( $request['amount'] > 0 ) return $reply; if ( isset( $request['user_id'] ) ) { if ( ! $mycred->exclude_user( $request['user_id'] ) ) { if ( $request_new <= 0 || $request_old <= 0 ) { return false; } } } else { return $reply; }}


    Currently, there is no possibility for stopping redirection on link click.

    Thread Starter marotiem

    (@marotiem)

    When I paste this code, it says syntax error, unexpected end of file. Could you please correct the code for me and resend it?

    A.Tariq

    (@arsalantariq)

    @marotiem,

    Please use the below-shared code also do let us know how it is working for you.

    add_filter('mycred_add', 'mycred_pro_no_negative_balance', 999, 2); function mycred_pro_no_negative_balance( $reply, $request ) { $mycred = mycred( $request['type'] ); // Ignore if allready declined if ( $reply === false ) return $reply; $balance = $mycred->get_users_balance( $request['user_id'], $request['type'] ); $request_new = $balance ; $request_old = $balance - $request['amount']; if ( $request['amount'] > 0 ) return $reply; if ( isset( $request['user_id'] ) ) { if ( ! $mycred->exclude_user( $request['user_id'] ) ) { if ( $request_new <= 0 || $request_old <= 0 ) { return false; } } } else { return $reply; } }

    Thread Starter marotiem

    (@marotiem)

    Hi!

    This code you sent does not allow any kind of deduction. Even if the user has enough balance, no points are deducted.

    A.Tariq

    (@arsalantariq)

    @marotiem,

    Can you please make a small video of the whole scenario?

    Thread Starter marotiem

    (@marotiem)

    Hi! What I am trying to achieve is basically a point payment without a shopping plugin. I do not have an online store, but I want to give rewards for points to my clients. E.g. if a client completed a challenge, they receive 10 points. Then they can redeem their points on a page that contains reward items. If an item has a value of 10 or less, the client can obtain the item. If the item has a value of more than 10, the transaction would be declined. I found this code that is similar to what I need. I would like to ask, if it can be modified in a way that does not require a shopping plugin?

    Code link: https://codex.mycred.me/filters/mycred_decline_store_purchase/

    A.Tariq

    (@arsalantariq)

    @marotiem,

    Are you using the woo commerce plugin? What type of reward challenges/items are there on your website? The code snippet link provided by you works with woocommerce.

    Thread Starter marotiem

    (@marotiem)

    I am not using woocommerce, nor any types of shopping plugins. That is why I need some modification in the code. The rewards I have are meditation type videos I recorded myself. When a client completes an online course, they get points through mycred. What I want is: with the points they have they can unlock the links to the meditation videos. It should work as clicking on a picture of the video that has a certain point value, mycred checks if they have enough balance, and if they do, points will be deducted and they receive the link in an email. If they do not have enough points in their balance, a message pops up: Insufficient balance, and points will not be deducted.

    Hello @marotiem ,

    It seems like you need custom coding to fulfill your requirement based on your website.

    Thread Starter marotiem

    (@marotiem)

    Actually, I achieved everything I wanted with the Sell Content add-on. No custom code was needed. I have one more question. I do not understand how my balance is calculated. When I counted my points with the log entries I got a different number from what appeared in my balance. How is it possible? (I only use points).

    Hello @marotiem ,

    When you add amount without log entry it will only add in current balance but when you add amount in log entry it will add in both current and total balance. See the screenshot for reference.

    Screenshot : https://ibb.co/LNzdWc7

    Also, you can see the documentation for better understanding.

    Let me know if you have any query.

    Thread Starter marotiem

    (@marotiem)

    Thank you Daniyal. It cleared for me. Thank you for all your help.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘prevent negative balance mycred link’ is closed to new replies.