I have a form (id=107) that is linked to a Woocommerce product.
I want to update a custom user field for the post author when the purchase is successful.
How do I use cred_commerce_after_payment_completed to do this? If possible, can you please provide a sample code snippet to help?
Thanks in advance.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Amin,
Thank you for contacting our support forum.
Could you let me know what kind of field this is ? Also is it for the current users ?
Finally I would need to know the slug of the custom field that you would like to update.
Thanks,
Shane
Hi Shane,
It's a "Single Line" field. I want to add the text "Paid" in that field once the payment is complete.
This would happen for the currently logged in user.
The slug is "is-current-paid-member"
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Amin,
The code that is required to update this is.
add_action( 'cred_commerce_after_payment_completed', 'cust_update_user_field', 10, 1 );
function cust_update_user_field( $data ) {
$userid = get_current_user_id();
update_user_meta( $userid, 'wpcf-is-current-paid-member', 'Paid');
}
Please try adding this and let me know if this helps.
Thanks,
Shane
Thanks Shane. I want this to run only for the specific form with ID=107. Can you update the code accordingly, please?
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Amin,
You can try doing this.
add_action( 'cred_commerce_after_payment_completed', 'cust_update_user_field', 10, 1 );
function cust_update_user_field( $data ) {
if ($data['extra_data']['cred_form_id'] == 107){
$userid = get_current_user_id();
update_user_meta( $userid, 'wpcf-is-current-paid-member', 'Paid');
}
}
Please let me know if this one helps.
Thanks,
Shane
Hi Shane,
This code doesn't work. Can you please check the code to make sure it's correct.
Thanks,
Amin
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Amin,
That is strange as the code should work.
Would it be possible to gain access to the site that you are testing this on ?
Also to understand the debugging that is required, does the code work correctly when you submit the form and made your test payment before adding the conditional for a specific form ID ?
Please let me know as soon as possible so we can fix this for you 🙂
Thanks,
Shane