0

Can someone suggest some possible solution to how do I achieve the following in WordPress?

I have a custom post type named Deals. On each deal page, I want to have a button which upon clicking will show some coupon code on the popup and then that button will be disabled for that particular user forever.

I know how to create a button and show popup, but what will be the way to disable it for that particular user after that?

  • 1
    Is the user logged in? – fuxia Mar 8 at 15:39
  • Yes, the button is visible and functionable only for logged in users. – vipworks Mar 8 at 15:40
0

Use a user meta entry to store the information that this user has received the coupon already. Then check this user meta before you print the button:

Plugin code:

function coupon_button()
{
    $post_id          = get_the_ID();
    $user_id          = get_current_user_id();
    $coupons_received = (array) get_user_meta( $user_id, 'coupons_received', true );

    if ( in_array( $post_id, $coupons_received ) )
        return '';

    return get_submit_button( 'Get Coupon', '' );
}

function update_coupon_status()
{
    $user_id            = get_current_user_id();
    $coupons_received   = (array) get_user_meta( $user_id, 'coupons_received', true );
    $coupons_received[] = get_the_ID();
    update_user_meta( $user_id, 'coupons_received', $coupons_received );
}

Template code:

print coupon_button();

And when you show the coupon, use the update function:

update_coupon_status();
| improve this answer | |
  • but with this, the meta value will be updated globally for all posts. I want it to work on per post basis. So if user clicked button on Post A, only the button of Post A will be disabled. The button on post B should still be enabled. – vipworks Mar 8 at 15:59
  • @vipworks Indeed, my bad. :) See my update. – fuxia Mar 8 at 16:22
  • Thanks fuxia ! Looks like this will work.. I will try this. – vipworks Mar 8 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.