Skip Navigation

[Resolved] Automatically create a custom post during User registration

This support ticket is created 6 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Author
Posts
#1079737

I tried code mentioned here https://toolset.com/forums/topic/have-a-band-admin-role-to-manage-cpt-band-listing-page/

Everything worked fine until had to have payment option using Toolset Forms Commerce. User is being created but not the post after the payment has been done and the order is marked completed. I am using the following code

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    if ($form_data['id']==225)
    {
      $my_post = array(
        'post_title'    => $_POST['wpcf-user-company-name'],
        'post_status'   => 'draft',
        'post_author' => $post_id,
        'post_type' => 'company'
      );
          
      // Insert the post into the database
      wp_insert_post( $my_post );
    }
}

I tried "cred_before_save_data" , "cred_submit_complete" and "cred_submit_complete" too in place of "cred_save_data" still didnt work. How do I get it work with payments?

#1079810

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

Thank you for contacting our support forum.

You need to use the hook here
https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_order_completed

Thanks,
Shane

#1079876

Thanks for the reference Shane. But, it didnt work for me. Tried the following,

add_action( 'cred_commerce_after_order_completed', 'create_company_post', 10, 1 );
function create_company_post( $data ) {
    $cred_form_id = $data['extra_data'][0]['cred_form_id'];
    if ($cred_form_id==225)
    {
      $cred_post_id = $data['extra_data'][0]['cred_post_id'];
      $my_post = array(
        'post_title'    => get_post_meta($cred_post_id, 'wpcf-user-company-name', true),
        'post_status'   => 'draft',
        'post_author' => $cred_post_id,
        'post_type' => 'company'
      );
          
      // Insert the post into the database
      wp_insert_post( $my_post );
    }
}
#1080738

Shane. still need your help. Unable to figure out the solution.
Unfortunately I am noob when it comes to PHP 😐

#1081337

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Sasan,

Just out of curiousity, does the order on your backend says complete ? This hook will only work if the woocommerce order status is set to complete.

Can you check this and let me know ?

Thanks,
Shane

#1081341

Yes, the order in woocommerce says completed I did double check. New user is created but not the post.

#1081498

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Sasan,

Would you mind allowing me access to the website so get this working for you ?

Could you let me know the form that you are using to get this done as well?

Thanks,
Shane

#1081658

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Sasan,

It seems as though none of the cred commerce hooks are working. What I would like to do is to test this on my local environment and see if the same issue occurs.

Would you mind if I took one of the duplicator packages for testing ?

Please let me know.
Thanks,
Shane

#1081755

Sure Shane, if you think you can test better locally do take the duplicator package no issues.

#1083180

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Sasan,

Just an update on this. Initially I thought that it was an issue with the hook itself but creating a test site locally, the hook works fine.

I'm going to test with some of your plugins disabled locally and see the results.

Thanks.
Shane

#1083182

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

If you see a product, I was the one that added just for some testing.

#1083185

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Sasan,

Actually I have some Great News.

The hook actually works 🙂 the problem was that the wrong ID was being used on the hook for the form. In the hook ID 225 was being used when it should've been 675.

Please let me know if this helps.

Thanks,
Shane

#1083255

Oh yes you are right Shane, that was so dumb mistake on my side. I should have updated the id when I created a new form to test.

But, there is an issue. New post is bring created but with no title and no author! The new user created should have been author and the company name from the form should have been the post title.

#1083863

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Sasan,

Could you change get_post_meta to get_user_meta.

This should set the correct title. You also said the post author isn't getting set correct?

Please let me know.
Thanks,
Shane

#1083906
post.JPG

get_user_meta didnt work Shane. Please refer to the screen shot. The post is still created without title and no author assigned.