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
#1340961

Also getting this in the debug log:

[16-Sep-2019 10:15:37 UTC] PHP Notice: Object of class WP_Error could not be converted to int in /home/silicondsdemo/public_html/wp-content/plugins/cred-frontend-editor/library/toolset/cred/embedded/models/UserForms.php on line 487
[16-Sep-2019 10:15:37 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

#1340963

Here is my latest code:

add_action('cred_commerce_after_order_completed', 'create_business', 10, 1);
function create_business($data)
{   
	$cred_form_id = $data['extra_data'][0]['cred_form_id'];
	$cred_user_id = $data['user_id'];
    if ($cred_form_id == 396) {
	    
      $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' => $cred_user_id,
        );
 
    wp_insert_post( $new_post );	

    }
}

#1341049

Nigel
Supporter

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

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

WooCommerce suggests a hook you can use to automatically complete orders, which might make sense in this case where the product is free: https://docs.woocommerce.com/document/automatically-complete-orders/

But if that's the case, that when a user submits the registration form, they will go through the cart mechanism to provide details but the order process will be completed automatically, meaning the registration will complete, why not just use the cred_save_data or cred_submit_complete hooks to generate the business post?

#1341087

Hi there,

Does an order only complete with admin interaction? Or will an order complete if its a paid product (I have to add a paid one as well) and once the money is cleared woocommerce will update the order status?

If I do set this up to use cred_save_data - if they dont go through the woocommerce checkout, the account will be setup for them without any verification they completed.

Regarding getting this to work as we have discussed, why is the error log adding:

[16-Sep-2019 12:07:58 UTC] PHP Notice: Object of class WP_Error could not be converted to int in /home/silicondsdemo/public_html/wp-content/plugins/cred-frontend-editor/library/toolset/cred/embedded/models/UserForms.php on line 487
[16-Sep-2019 12:07:58 UTC] PHP Notice: Undefined index: wpcf-company-name in /home/silicondsdemo/public_html/wp-content/themes/silicon-child/inc/toolset.php on line 90
[16-Sep-2019 12:07:58 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

Is this a bug?

#1341221

Nigel
Supporter

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

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

I believe for paid products WooCommerce will automatically complete the order upon payment (I always use checks and manual order processing in testing).

So, from what you say we should proceed with the cred_commerce API hooks.

For the purposes of testing you can manually update the order status in the backend to complete the process until such time as you set up online payment.

That should still trigger the creation of the custom post using your code.

But something is amiss, producing the PHP error.

Can I take a look at your set-up to see if I can spot anything?

You are adding your custom code in a file toolset.php in your theme, is that right?

Let me set up a private reply to get site credentials, and can you confirm the user form we are working with and where it is inserted?

#1341359

Hi there,

Can you get back to me on this please?

Even this creates an error when I manually set an order to completed.

add_action('cred_commerce_after_order_completed', 'func_update_user_role_custom',10,1);
  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'];
        $user = get_userdata( $user_id );
        $user->set_role( 'non_paid_supporter' );
    }
}
#1341749

Nigel
Supporter

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

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

Hi Paul

I've been trying to replicate the errors on a local test site with a similar set-up but cannot, so I'm taking a copy of your site so that I can do some further testing.

I did notice in my testing that there does appear to be a problem using the cred_commerce hooks, inasmuch as on my site the user_id returned is always zero, meaning there is no way to access the user data for the newly registered user. I'll be reporting that anyway, but I'll update when I've installed a local copy and checked it.

#1341777

Nigel
Supporter

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

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

Paul, I can't see where the registration form is added, it's not on the page Registration form, for example.

#1341819

Hi there ,

It seems to be a bug with cred_commerce_after_order_completed. When I swap it with payment_complete hook I'm not getting the errors in the log. However, I still havent found a way to get the company name 🙁

#1341907

Nigel
Supporter

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

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

Where is the registration form displayed?

#1342065

Hi there,

I have removed everrything from the form apart from the Submit button. This now links straight through to the checkout and the product linked to the form where they fill out there details and it creates the account for them.

hidden link

You will see how this works. There is a content template called Membership Table where the forms are (now just a button at the bottom).

I am still convinced there is a bug in Commerce Forms. No matter which handle I use this happens in the error log:

[17-Sep-2019 12:11:37 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

#1342067

This is all I am doing in the functions to cause the error:

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' ) );
       	$my_post = array(
			  'post_title'    => 'My Company',
			  'post_content'  => '',
			  'post_status'   => 'publish',
			  'post_author'   => $user_id,
			  'post_type' => 'business'
);
// Insert the post into the database
wp_insert_post( $my_post );			
       }		
    };
#1342183

Nigel
Supporter

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

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

I'm looking through the copy of your site I'd already installed and I still can't work out where the register form is displayed.

Unfortunately there isn't a scan button for forms, and without knowing where the form is added I can't proceed.

The /memberships page uses nested content templates that lead to this:

<table>
  <tr>
    <td></td>
    <td>Learn More <a href="/membership/non-paid-supporter/">REGISTER</a></td>
    <td>Register <button class="btn btn-primary">Regsiter Now</button></td>
  </tr>
</table>

but there is no page at "/membership/non-paid-supporter/" as far as I can see, and the register button is a button without any obvious action.

#1342623

Hi there,

I have made various changes to the site - maybe you have an older copy.

The content template with the Membership table is: Page Element: Membership Table

The button that says REGISTER under the FREE account is a CRED submit button which then takes them straight through the checkout process. Its when they complete there order and then it tries to fire the cred_commerce_after_payment_completed that I get a load of errors.

I'm wondering - is the function trying to find a variable / array but the User / product is coming back as empty at that stage? Is there a way to capture errors in the function or can we solve the bug?

Many thanks for your persistence..

Paul

#1342655

Nigel
Supporter

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

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

It's the copy I took on Monday, but let me grab an updated copy so I'm looking at the same thing.

I have filed an issue about the cred_commerce_after_order_completed hook seemingly not working as expected (the user id is not available, it returns zero), but I'm not seeing any of the errors you report on your site. (Nor when checking the cred_commerce_after_payment_completed hook.)

I'll check the updated version of your site and get back to you.

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