Problem:
The user collects payment for user registration, he would like to automatically create a post from a custom post type when the user completes the payment. And he would like to pass some data(custom fields) in the user registration form.
Solution:
This requires:
The cred_commerce_after_order_completed hook passes a $data object to the hooked function. The order_id is the "transaction_id" in the $data object. The created user is saved in a custom field of the order, called "_cred_post_id". Then we can pull the user fields from the user, create the custom post, and save the custom field values for the post.
Check this sample code:
add_action( 'cred_commerce_after_order_completed', 'my_cred_commerce_after_order_completed', 10, 1 ); function my_cred_commerce_after_order_completed( $data ) { // get the order id $order_id = $data['transaction_id']; // get the user id $user_id = get_post_meta( $order_id, '_cred_post_id', true); // get user data to have a title for the post. $user = get_userdata( $user_id ); // prepare post object $post = array( 'post_type' => 'user-profile', // <== CPT slug 'post_title' => $user->user_login, // <== post title 'post_status' => 'publish', 'post_author' => $user_id, // <== Author ); // Insert the post into the database $post_id = wp_insert_post( $post ); // get user field $phone = get_user_meta( $user_id, 'wpcf-phone-number', true ); $agency = get_user_meta( $user_id, 'wpcf-agency1', true ); $speciality = get_user_meta( $user_id, 'wpcf-specialty', true ); // create post field update_post_meta( $post_id, 'wpcf-agency1', $agency ); update_post_meta( $post_id, 'wpcf-phone-number', $phone ); update_post_meta( $post_id, 'wpcf-speciality', $speciality ); }
Relevant Documentation:
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)
Ce sujet contient 11 réponses, a 2 voix.
Dernière mise à jour par Il y a 4 années et 5 mois.
Assisté par: Jamal.