Skip Navigation

[Resolved] Add a custom post after order complete

This support ticket is created 4 years, 7 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
- 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+01:00)

This topic contains 46 replies, has 2 voices.

Last updated by Nigel 4 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#1342693

Awesome thank you!

Yes the weird thing is - it must be grabbing a user_id as its assigning the new post to the registering user. There must be something filling a response with an Error array as per the debug log I sent.

Hopefully you can replicate the issue and we can come up with a workaround 🙂

Thanks so much for your continued support

Paul

#1342749

Nigel
Supporter

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

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

Hi Paul

I've identified the problem, but don't have a workaround at the moment, I need to get some clarification from my colleagues about what is intended.

The problem is that your user registration form is just a submit button.

It doesn't contain any fields, just the submit button, although you are auto-generating the username and password.

But in my testing it seems that the email address is also required.

If you add the email address the errors will disappear.

I can see why you've done this. The client details need to be entered on the WooCommerce page and you don't want to enter them twice, but it looks like that may be required.

Plus, I'm also waiting for feedback on whether the hooks are actually working correctly, because they are returning data with user_id = 0 instead of the actual user ID.

I'm not done, I'll keep updating you.

#1342771

UPDATE:

Hi there,

I resolved my initial issue - It's now creating the post with the appropriate name as entered in the checkout. I gave up on trying to get the info from the CRED User form. There seems to be an issue with this.

Here's my working code:

add_action('cred_commerce_after_payment_completed', 'func_update_user_role_nonpaid',10,1);
  function func_update_user_role_nonpaid( $data ) {
    if ( isset($data['extra_data'][0]['cred_form_id']) && $data['extra_data'][0]['cred_form_id'] == 396) {
        
        $user_id = $data['user_id'];
        $user_id = wp_update_user( array( 'ID' => $user_id, 'role' => 'non_paid_supporter' ) );
        
          $usercompany = get_user_meta(  $user_id, 'billing_company', true);
         
       	$my_post = array(
			  'post_title'    => $usercompany,
			  'post_content'  => '',
			  'post_status'   => 'publish',
			  'post_author'   => $user_id,
			  'post_type' => 'business'
);
// Insert the post into the database
wp_insert_post( $my_post );			
       }		
    };

HOWEVER, I am still getting the bug in my error_log:

[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

#1342779

Nigel
Supporter

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

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

Are you seeing those errors after adding an email field to your user registration form?

#1342783

Hi there,

The error instantly and only occurs when the user finishes checkout and it tries to fire either the cred_commerce_after_order_completed OR cred_commerce_after_payment_completed.

#1342785

Further to this - the error occured whether I had anything in the CRED User form (Commerce option selected) or not.

I now have just a SUBMIT button for each from which takes them through the checkout.

The reason is that it's pointless getting them to input info twice - I found that whatever happened, linking the Create User form with a Commerce Product - any info they put in to the Woo checkout fields would over-right all the new User info anyway....

#1342787

I'm guessing that this side of the development needs some looking into? I.e. Linking a New User form to Commerce. I'm sure it all works when it's a normal post form made to be commerce and linked to a product.....but I'm only guessing.....

#1342789

Nigel
Supporter

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

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

I've asked my colleagues to look into it further and to clarify what is expected to work and verify whether it does, so now I'm waiting to hear back from them.

#1346057

Hi there,

Is there any update on this? The CRED COMMERCE hooks are still generating errors for SAVE PAYMENT and ORDER COMPLETE.

Is there a patch or fix coming soon for this?

Many thanks

#1346077

Nigel
Supporter

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

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

I spoke with the devs about this this morning and it is being worked on this week, we will hopefully have a patch or update available within the next few days.

I'll let you know when.

#1347327

Nigel
Supporter

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

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

Hi Paul

I see some updates in the escalated tickets relating to this, which unfortunately I don't think you are going to like.

The Forms Commerce API hooks when used with user forms do not make the user_id of the newly registered user available, and according to the developers this is expected. The user_id is zero because it is the ID of the user that submitted the form, which would be a guest user, hence user_id = 0.

There is currently no way to get the ID of the user created by this process using the Forms hooks, and hence no way to do what you are aiming to do, namely create a custom post using the API where the author is this user, because we don't know who the user is.

Having the user ID of the new user available requires a request to update the API (either update the existing hooks or add a new hook), which you'll need to submit via the request form: https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

The error seems to arise because the email address is missing from the user registration field. Forms will be updated to make it a required field on forms that create users (it will be optional on edit user forms).

The testing around this has unearthed a couple of issues, but they are not directly related to your question, and resolving them won't help you with this particular problem.

Sorry, but based on the feedback from the developers, I don't have a solution for you.

#1348007

Hi there Nigel,

Unfortunately I totally disagree with the answer given above - maybe the developers got the wrong end of the stick. This doesn't resolve the issue.

The fact is - IT IS creating a new user and assigning a new post to that user. IT IS catching the new user ID in time and It all works perfectly. The problem is with the error handling meaning it's not smoothly logging the new user in at the end of the process - it relies on a page refresh.

Is there a way I can handle the specific error in my code - i.e. a condition in my code that will catch it. You can test yourself by going to the account page and creating an account. You will see that the error happens when the user reaches the order confirm page. Hitting refresh means you are logged in as the new user with the new post ready to edit.

hidden link

I just want to get rid of the intervening page and the user having to refresh because the error isn't being caught and dealt with.

I appreciate your further advice once you have seen the process via the link above...

Paul

#1348291

Nigel
Supporter

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

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

Hi Paul

I'm not quite sure how it is working in your case, because the extensive testing I have done and my colleague in second tier has done indicate that the user_id is not available with any of the cred_commerce API hooks, and the developers have stated that the user_id of the newly registered user is not available with any of the current hooks, and to make it available would require a feature request.

Let me also repeat what I wrote above, that the email address is a required field in the user registration form.

Now, I know that I've had something along these lines working in the past, despite the statements by the developers, so I persisted in testing myself, starting from scratch, and I've identified how I was able to work with the user_id previously.

Despite not being available directly from the API hooks, the user id gets recorded in a hidden custom field on the WC Order post, with a key of '_cred_post_id'. (With post forms it is the post id, but with user forms this is the user id.)

So, if we use the hook cred_commerce_after_order_completed we have the transaction_id available (which is the id of the WC order), and so we can retrieve the user id from that.

So, in my plain vanilla test site where I have a user registration form with one custom field (company name) and where the username etc. are auto-generated, but which otherwise includes the defaults, then this code works to generate the post based upon the customer name, without error:

/**
 * Use the user ID for a newly registered user via WooCommerce
 */
function ts_new_user_id( $data ){

    if ( isset( $data['transaction_id'] ) ) {
        $order_id = $data['transaction_id'];
        $user_id = get_post_meta( $order_id, '_cred_post_id', true );

        if ( isset( $user_id ) ){

            // Now we have the user ID we can do whatever with it
            $company_name = 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 );
        }
    }
}
add_action( 'cred_commerce_after_order_completed', 'ts_new_user_id' );

So, where do the errors you describe come in?

Well, all I have to do on my test site to generate the same errors is to remove the email field from the user registration form, then I get the same errors you report.

But the email address is required in a form to register a user. (The GUI lets you delete it, but will be changed so that you cannot.)

Including the email in the user registration form will eliminate the errors.

#1348383

Hey there Nigel,

So many thanks for all your work with me on this - your awesome!

Ok well I changed my code based on your suggestions. It seems that it works on Payment complete rather than order_complete....interesting!

So how I solved my issue was to hide an email field on the forms with a default address of mail@mail.com. Woocommerce overwrites this anyway with what they enter on the checkout screen.

Now it passes through without any error. No error log. It must have been looking for the email as you say.

If you dont mind I will do further testing on order_complete and see if I can get it working then post back on here so that we have a complete solution for anyone else reading in.

Thanks

Paul

#1350181

Nigel
Supporter

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

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

This is sitting in my queue, so let me just update the status to awaiting feedback.

Let me know how you get on.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.