Skip Navigation

[Resolved] WooCommerce Order Number in Entry Form Notification Email

This support ticket is created 3 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 15 replies, has 4 voices.

Last updated by janeC-2 3 years, 3 months ago.

Assisted by: Jamal.

Author
Posts
#2175653

I have created an entry form which goes straight to the checkout to pay for the entry.

I would like to include the WooCommerce order number in the Admin Notification Form body and subject line.

I tried following advice on the following page but it's not working for me: https://toolset.com/forums/topic/how-can-i-see-number-of-order-for-the-form-with-payment/

I created a Custom Field in the Entries CPT called woo-order-number

I added the following to custom code

add_action( 'cred_commerce_after_order_completed', 'save_order_num', 10, 1 );
function save_order_num( $data ) {
//some code here
if($data['extra_data']['cred_form_id'] == 49 ){

$order_num = $data['transaction_id'];
update_post_meta($data['extra_data']['cred_post_id'],'wpcf-woo-order-number',$order_num );
}

}

NOTIFICATION EMAIL:
I added this to the Subject line
[types field='woo-order-number'][/types]

I'm not sure how to get the field to show in the body of the notifications email. Will it show if I use
%%FORM_DATA%%

#2175981

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Jane

I think the solution here may be the same as in the other ticket.

Assuming the custom code works correctly (you could check in the database to see that the order number is being saved in wp_postmeta), then you likely need to add the item="%%POST_ID%%" attribute to the types shortcode in the notification to anchor the source to the correct post.

#2176003

Hi Nigel,
The field I want is the WooCommerce Order Number, not the Post ID

I don't think the custom code is correct. I added a plain text field to the Entries CPT and its not appearing in the post.

#2177257

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

It seems the the hook "cred_commerce_after_order_completed" at last and the email is rendered before the "cred_commerce_after_order_completed" hook is called but I can tell once I can review it.

Can you please share from where I can place the order and allow me to place dummy order with cheque or any other payment method.

Please share problem URL and admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2178593

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I can see the form and fill it up but when I click on submit button it just not redirect me on checkout page. I give couple of tries, it creates a new entry post but it does not redirect me on checkout page. I see you are using cache plugin as well, for now could you please disable it.

Do I require to login as non-admin user? If yes, can you please send me non-admin user using which I should place the order.

#2179099

Hi,
Sorry, can I send you login for the live site instead of the test site I set up?
It is still a demo site so you can make changes without affecting anything.
I have a backup
Login and FTP are the same for you except the URL is hidden link

#2180609

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I tried the admin access details you shared previously for the site hidden link but unfortunately the shared admin access details is not working.

Can you please send me working admin access details.

Also, have you configured your form for the post status - please review the following doc:
=> https://toolset.com/course-lesson/charge-users-for-submitting-content/#3-what-happens-when-payments-complete

#2180807

Hi Minesh,
I went back to the private message and updated the admin access details for you.

Jane

#2181489

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I see you still not fully configured your form:
=> hidden link

Please check the section "Post status when the payment status updates" - can you please set the correct status.

More info:
=> https://toolset.com/course-lesson/charge-users-for-submitting-content/#3-what-happens-when-payments-complete

#2181491

I've updated it now

#2181521

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - you can take over.

#2184225

any update on this?

I have created CPT called Entries and added a field called woo-order-number

The post form creates a post in Entries and takes the person to the checkout to pay using Woocommerce.
I want the WooCommerce order number to appear in the notification email or in the post.

I have added this custom code:
// Put the code of your snippet below this comment.
add_action( 'cred_commerce_after_order_completed', 'save_order_num', 10, 1 );
function save_order_num( $data ) {
//some code here
if($data['extra_data']['cred_form_id'] == 49 ){

$order_num = $data['transaction_id'];
update_post_meta($data['extra_data']['cred_post_id'],'wpcf-woo-order-number',$order_num );
}

}

But the order number is not coming across to the Post or the notification email.

#2186937

Hello Jane, and our apologies for the late reply.

Well, you won't be able to get the order ID in the notification email. Because the order is created after the notification is sent. The current notification is sent on form submit, this is what happens:
1. User submits the form.
2. Toolset creates the post entry.
3. Toolset sends the notification.
4. The form submission completes, and the user is redirected to checkout.
5. After the user completes the checkout, the order is created.
Basically, you want to get the order ID, that is created at step 5, in step 3. Which is just not possible.

On the other hand, when the order is complete, the custom code runs and should save the order ID on the post's custom field "Woo Order Number". However, the code is not correct. It does not get the form_id, or the post_id. Try this code instead:

I figure it out after running a couple of tests. I have created a new form(#407), product, and page to make things simple. And I was able to save the order ID after the order completes with the following code:

add_action( 'cred_commerce_after_order_completed', 'save_order_num', 10, 1 );
function save_order_num( $data ) {
  //some code here
  $forms = array( 49, 407 );
  if( in_array( $data['extra_data'][0]['cred_form_id'], $forms ) ){
    $order_num = $data['transaction_id'];
    update_post_meta($data['extra_data'][0]['cred_post_id'],'wpcf-woo-order-number', $order_num );
  }
}

Notice how I added the [0] on lines 5 and 7.

To summarize:
- You cannot send the order ID with a notification that is sent before the order is even created.
- There was an error with the custom code that should save the order ID in the post's custom field when the order completes(cred_commerce_after_order_completed).

I hope this helps. Let me know if you have any questions.

#2187243

Hi,
Thanks for the clever solution.

However, there's now a bug with the confirmation email - the data from the form is not coming across in the confirmation email now.
I'm a bit worried because it is now live.

UPDATE:
I deactivated the custom code and the confirmation email data came across.
I'm just looking at what could be causing these fields to not come across in the confirmation email.
example:
Name: [types field='fname' item='%%POST_ID%%'][/types] [types field='lname' item='%%POST_ID%%'][/types]

used to show the fields, now it just shows
Name:

UPDATE 2:
There is additional code beyond what you have included in your reply.
Should all this still be there?
Also, is it possible to write the order number when the order status is 'On Hold' - This helps us to follow up on people who haven't yet paid.

// Put the code of your snippet below this comment.
add_action( 'cred_commerce_after_order_completed', 'save_order_num', 10, 1 );
function save_order_num( $data ) {
//some code here
$forms = array( 49, 407 );
if( in_array( $data['extra_data'][0]['cred_form_id'], $forms ) ){
$order_num = $data['transaction_id'];
update_post_meta($data['extra_data'][0]['cred_post_id'],'wpcf-woo-order-number', $order_num );
}
}

add_action( 'cred_commerce_after_order_completed', 'my_cred_commerce_after_order_completed', 10, 1 );
function my_cred_commerce_after_order_completed( $data ) {
error_log('my_cred_commerce_after_order_completed');
error_log( print_r( $data, true ) );
}

add_filter('cred_subject_notification_codes', 'my_field_notification', 999, 3);
add_filter('cred_body_notification_codes', 'my_field_notification', 999, 3);

function my_field_notification( $defaultPlaceHolders, $form_id,$post_id ) {
if( $form_id == 407 ) {
// form data
$form_data = get_post($form_id,ARRAY_A);
// post data
$post_data = get_post($post_id,ARRAY_A);

$newPlaceHolders['%%MY_FORM_DATA%%'] = '<pre>MY_FORM_DATA: ' . print_r( $form_data, true ) . '</pre>';
$newPlaceHolders['%%MY_POST_DATA%%'] = '<pre>MY_POST_DATA: ' . print_r( $post_data, true ) . '</pre>';

error_log('my_cred_notification_codes');
error_log( print_r( array(
'form_id' => $form_id,
'post_id' => $post_id,
'form_data' => $form_data,
'post_data' => $post_data,
), true ) );

return array_merge($defaultPlaceHolders, $newPlaceHolders );
}
}

#2187561

I was using the other parts of code during my debugging, and I wanted to remove them. I must forget to hit on save.
Those codes are probably the cause behind the issue with the confirmation email. Let's remove them.

I removed those codes and I wonder if the issue with the confirmation email is still present?

Regarding your question is it possible to write the order number when the order status is 'On Hold' - This helps us to follow up on people who haven't yet paid
Toolset does not offer a hook to be used in this case. But, you can use the default WooCommerce hooks, such as woocommerce_new_order. I did not use it, so I can't tell the code to use, but I'll give it a try later today.
hidden link