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 );
}
// }
};
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.
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.
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
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.
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:
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"?