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?
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?
Share Improve this question edited Mar 9, 2020 at 11:34 sleepingpanda 566 bronze badges asked Mar 8, 2020 at 15:10 vipworksvipworks 731 silver badge9 bronze badges 2- 1 Is the user logged in? – fuxia ♦ Commented Mar 8, 2020 at 15:39
- Yes, the button is visible and functionable only for logged in users. – vipworks Commented Mar 8, 2020 at 15:40
1 Answer
Reset to default 0Use 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();