Hi there,
I have setup User fields including Company name.
I have a User form that creates a new user. However when this happens, I'm getting it to create a custom post with the name given when the new user fills out the form.
The problem is its creating the new post with (no-title). I.e. its not retrieving the field. Maybe its a problem with my syntax....
Here's my code:
add_action('cred_commerce_after_payment_completed', 'func_update_user_role_custom',10,2);
function func_update_user_role_custom( $data ) {
if ( isset($data['extra_data'][0]['cred_form_id']) && $data['extra_data'][0]['cred_form_id'] == 396) {
$user_id = $data['user_id'];
$post_id = $data['extra_data'][0]['cred_post_id'];
$user_id = wp_update_user( array( 'ID' => $user_id, 'role' => 'non_paid_supporter' ) );
// $usercompany = get_post_meta($post_id, 'wpcf-company-name', true);
$usercompany = sanitize_text_field(get_user_meta($post_id, 'wpcf-company-name', true));
$new_post = array(
'post_title' => $usercompany,
'post_type' => 'business',
'post_content' => '',
'post_status' => 'publish',
'post_author' => $user_id
);
wp_insert_post( $new_post );
}
}
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Paul,
Thank you for getting in touch.
I see a way we can test this. Is the code setting the correct user ID with the $user_id variable?
Since you are retrieving a user field then it needs to be like this.
get_user_meta($user_id, 'wpcf-company-name', true)
Please let me know if this helps.
Thanks,
Shane
Hi there,
Yes I swapped ur version in. Still producing (no_title). The user_id is being retrieved correctly - because its assigning the new post to the correct user....
Is there anything else I can try?
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Paul,
I suspect the issue is here
$usercompany = sanitize_text_field(get_user_meta($post_id, 'wpcf-company-name', true));
Would you mind allowing me to have admin access to the site so that I can have a look at this one for you ?
Then private fields have been enabled for your next response.
Thanks,
Shane
P.s. You will need to access the toolset.php where the functions are via the Theme Editor > Inc/ folder
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Paul,
I believe I found the issue now. If you are creating the users account by just clicking the button here hidden link
Then you are redirected to the woocommerce checkout page here
hidden link
There is nothing to fill out on the actual CRED form so your data is being entered into the woocommerce checkout page. All the fields here are woocommerce fields so you will need to get the information from the woocommerce custom user fields.
So in your case it should be.
$usercompany = get_post_meta($user_id, 'billing_company', true);
Please update your code with this and let me know if it helps.
Thanks,
Shane
Hi - I am still getting the (no_title). Is there another combination I can try / variation on the variable like:
$user_id = $data['user_id'];
$userfield = $data['user_id'];
$post_id = $data['extra_data'][0]['cred_post_id'];
$user_id = wp_update_user( array( 'ID' => $user_id, 'role' => 'non_paid_supporter' ) );
$usercompany = get_post_meta($userfield, 'billing_company', true);
OR
$usercompany = get_post_meta( $post_id, 'billing_company', true);
OR
$usercompany = get_user_meta( $user_id, 'billing_company', true);
SORRY!!!
THis is now resolved!!
I used: $usercompany = get_user_meta( $user_id, 'billing_company', true);
However still getting debug errors:
[18-Sep-2019 10:03:17 UTC] PHP Fatal error: Uncaught Error: Cannot use object of type WP_Error as array in /home/silicondsdemo/public_html/wp-content/plugins/cred-commerce/classes/CRED_Commerce_Forms_Meta_Handler.php:52
Stack trace:
#0 /home/silicondsdemo/public_html/wp-content/plugins/cred-commerce/classes/event/base.php(36): CRED_Commerce_Forms_Meta_Handler->get_forms_meta_data('cred_meta')
#1 /home/silicondsdemo/public_html/wp-content/plugins/cred-commerce/classes/event/onorderchange.php(11): CRED_Commerce_Event_Base->execute()
#2 /home/silicondsdemo/public_html/wp-content/plugins/cred-commerce/classes/Form_Handler.php(213): CRED_Commerce_Event_OnOrderChange->execute()
#3 /home/silicondsdemo/public_html/wp-content/plugins/cred-commerce/classes/Plugin_Base.php(47): CRED_Commerce_Form_Handler->onOrderChange(Array)
#4 /home/silicondsdemo/public_html/wp-content/plugins/cred-commerce/plugins/woocommerce/CRED_Woocommerce.php(195): CRED_Commerce_Plugin_Base->dispatch('_cred_order_sta...', Array)
#5 /home/silicondsdemo/public_html/wp-includes/class-wp-hook. in /home/silicondsdemo/public_html/wp-content/plugins/cred-commerce/classes/CRED_Commerce_Forms_Meta_Handler.php on line 52
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Paul,
Not sure what could be causing this error.
Was it there before we fixed the hook or after ?
Please let me know.
Thanks,
Shane
Hi there,
I have raised a similar query here which has been escalated...will mark this one as closed:
https://toolset.com/forums/topic/add-a-custom-post-after-order-complete/
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Paul,
Awesome hopefully Nigel is able to get this next issue resolved for you.
You may mark this one as resolved.
Thanks,
Shane
My issue is resolved now. Thank you!