Home › Toolset Professional Support › [Resolved] Add a custom post after order complete
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | - |
- | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | - |
Supporter timezone: Europe/London (GMT+00:00)
Tagged: CRED API, Toolset Forms, User-registration forms
Related documentation:
This topic contains 46 replies, has 2 voices.
Last updated by Nigel 5 years, 1 month ago.
Assisted by: Nigel.
Hello there,
Please see the custom function I have below. Basically I have a User Form (with the Charge For tick box selected) that adds a user when they purchase a product.
The problem is I think I'm getting something wrong in the code below as no post is created. I'm wondering whether I need to use a CRED Commerce hook or a standard CRED hook:
/* Registration form - Set the Username and Nickname to company-firstname-lastname */ add_action('cred_commerce_after_order_completed', 'my_save_data_action',10,1); function my_save_data_action( $user_id,$data) { // if a specific form if ($data['cred_form_id']==396) { global $wpdb; $my_post = array( 'post_title' => $_POST['wpcf-company-name'], 'post_content' => '', 'post_status' => 'publish', 'post_author' => $user_id, 'post_type' => 'business' ); // Insert the post into the database wp_insert_post( $my_post ); } // } };
Wonderful! Thanks for your assistance.
Yes basically the user clicks Register > Get taken to a CRED USER FORM > Then through checkout process for product > then auto logged in (using wp-members code in functions) > its at the point when the order is completed the post must be generated as they get redirected straight to a dashboard where they can edit said post.
Excitedly waiting your response....
Paul
And yes - Company name is a user fields that I want as the title of the new post.
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Paul
I didn't test it, but reading the documentation for the filter I think this should do what you need:
add_action('cred_commerce_after_order_completed', 'tssupp_publish_post', 999, 1 ); function tssupp_publish_post( $data ){ // confirm originating user form if ( $data['extra_data']['cred_post_id'] == '123' ) { // Edit 123 $company_name = get_user_meta( $data['user_id'], 'wpcf-company-name', true ); $new_post = array( 'post_title' => $company_name, 'post_type' => 'business', 'post_content' => '', 'post_status' => 'publish', 'post_author' => $user_id ); wp_insert_post( $new_post ); } }
When the order is completed if the originating form ID matches then it will get the company-name custom field of the user registered by the form and use it to make a new business post using that as the title.
Can you try that?
Hi there!
Sadly this isnt working. My original code I supplied worked perfectly on CRED SAVE DATA hook. I get what you say about the new hook is different (gets data differently).
Just one thing, I made a small change to see if that worked (was I correct in doing this? It still wont work):
if ( $data['extra_data']['cred_form_id'] == '396' ) { // Edit 123
So now it reads:
add_action('cred_commerce_after_order_completed', 'tssupp_publish_post', 999, 1 ); function tssupp_publish_post( $data ){ // confirm originating user form if ( $data['extra_data']['cred_form_id'] == '396' ) { // Edit 123 $company_name = get_user_meta( $data['user_id'], 'wpcf-company-name', true ); $new_post = array( 'post_title' => $company_name, 'post_type' => 'business', 'post_content' => '', 'post_status' => 'publish', 'post_author' => $user_id ); wp_insert_post( $new_post ); } }
This is relevant.....
https://toolset.com/forums/topic/cred-commerce-api-hooks-not-firing/
Can you help me based on the above post?
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Ah, it's an array of arrays.
That line should be
if ( $data['extra_data'][0]['cred_form_id'] == '396' ) {
Can you try again?
Hi there,
Yes had tried that and still nothing - no post created. Please see further amend to code below. Im wondering if we are getting the right user id or if its failing trying to get a piece of info it needs.
add_action('cred_commerce_after_order_completed', 'tssupp_publish_post', 10, 1 ); function tssupp_publish_post( $data ){ // confirm originating user form if ( $data['extra_data'][0]['cred_form_id'] == '396' ) { // $cred_post_id = $data['extra_data'][0]['cred_post_id']; $user_id = $data['user_id']; $usercompany = sanitize_text_field(get_user_meta($user_id, 'wpcf-company-name', true)); $new_post = array( 'post_title' => $company_name, 'post_type' => 'business', 'post_content' => '', 'post_status' => 'publish', 'post_author' => $user_id ); wp_insert_post( $new_post ); } }
Just to reiterate - this is a user form creating a new user...which is linked to a product in Woocommerce.
I reckon the normal CRED SAVE DATA hook would still work on the User form. It's just not working on the ORDER COMPLETE....
Thanks again for your continued help with this.
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Are you using Toolset Forms Commerce 1.8.1? (That fixed an issue where the API hooks weren't firing.)
Are you comfortable turning on and checking the debug.log?
You can sprinkle some messages to the log through the code which would help indicate where it is failing, e.g.
add_action('cred_commerce_after_order_completed', 'tssupp_publish_post', 10, 1); function tssupp_publish_post($data) { error_log("CODE SNIPPET RUNNING"); if ($data['extra_data'][0]['cred_form_id'] == '396') { error_log("PASSED FORM ID TEST"); $user_id = $data['user_id']; $usercompany = sanitize_text_field(get_user_meta($user_id, 'wpcf-company-name', true)); $new_post = array( 'post_title' => $company_name, 'post_type' => 'business', 'post_content' => '', 'post_status' => 'publish', 'post_author' => $user_id, ); $inserted_post = wp_insert_post($new_post, true); error_log('inserted_post: ' . print_r($inserted_post, true)); } }
Hi there,
Nothing is being written to the log....not even the error_log bits! I just can't work it out. I'm running all the latest toolset.
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
OK, I need to check on a test site to verify whether the hooks are being fired.
In the meantime you may need to use the cred_save_data hook (though this will mean the posts being created even if the user doesn't complete the sign-up, which is presumably why you want to use the cred commerce hook).
I'm in the middle of some other testing at the moment, I'll get to this after that.
Sorry to disturb you, I know you said you'd come back and help. Just thought I'd paste in the latest version of the code which I think should be as correct as possible:
add_action('cred_commerce_after_order_completed', 'create_business', 10, 2); function create_business($data) { if ($data['extra_data'][0]['cred_form_id'] == '396') { $cred_post_id = $data['extra_data'][0]['cred_post_id']; $data = array( 'user_id' => $user_id ); $usercompany = sanitize_text_field(get_user_meta($user_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 ); } }
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
I managed to run a test just now, and the hooks are being triggered as expected.
Here is a minimal version of the code to test that:
function tssupp_on_order_complete($data) { error_log('data: ' . print_r($data, true)); } add_action('cred_commerce_after_order_completed', 'tssupp_on_order_complete', 10, 1);
Where are you adding your code? Is it active?
Your debug logs are turned on?
Hi there,
I've worked out why the code isnt firing. The issue is that the product is FREE. The user is filling out their details and completing the order through woocommerce - however the order status is stuck on "Processing"....so the code wont fire as it's looking for a completed order.
What can I do to make the order update automatically with "Complete"?
Many thanks