Hi,
I am facing the following problem that I can't really wrap my head around on how to solve.
My goal is:
1. Have users register via custom registration form and assign a custom role (no problem on this one)
2. Each user represents a business that may have various locations
3. Users will buy subscriptions (handled by WooCommerce Subscriptions Plugin)
4. Users need to be able to add all of their locations via a cred form (child posts?)
5. I need to create an order form for the subscriptions where the user can select a location of his, and the subscription he wants for that location. He should be able to select a subscription for each location he has.
So basically that order form is a product that will show the locations of the logged in user and let him select a subscription for each location. I realize that in the end all the subscriptions will be added to his account but for his internal management he needs to be able to know which location has which subscription.
Any help on this would be highly appreciated
Thanks! 🙂
I'm not an expert in WooCommerce Subscriptions, so I'm not able to offer much guidance on that part of the process. I can help you implement any part of the process that can be accomplished with our plugins. Here's one way I can see this working with Toolset:
1. Create a Business post type, a Location post type, and a Subscription post type. Location is a child of Business. Subscription is a child of Location. More information about one-to-many relationships here: https://toolset.com/documentation/toolset-training-course/part-8-one-to-many-relationships-in-toolset/
2. When a user registers, automatically create a single Business for each User and make that User the author of the Business.
3. When a User logs in, send them directly to their Business single post page. On this page you should have a View showing all their Locations and a CRED form where they can create new Locations.
4. Once the User submits the CRED form and creates a Location, the Location View will update to include information about each Location. In each item of the View, include a link to a child CRED form that says something like "Purchase a Subscription for this Location". This will be the CRED Commerce form that selects an existing Product from your shop and adds it into the User's Cart. You can make it select the same Product every time, or you can allow the User to select their desired Product - like a shorter or longer subscription. More information about that here:
https://toolset.com/documentation/user-guides/using-cred-commerce-to-add-payments-to-forms/
5. When the User completes payment, the Subscription post that is created by CRED will be updated. Typically this is set to change from a Pending or Draft post to a Published post. You can then set up conditional HTML that will display each published Subscription next to its parent Location in the View on the Business single post page, instead of the link to create a new child Subscription. More about conditional HTML here: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
6. What happens after this is not quite as clear to me, because much of it depends on how WC Subscriptions manages subscription expiration and renewal. It seems like you need to know when a subscription in WC Subscriptions expires, or is cancelled manually, in order to update the Subscription post created by CRED. If they offer action hooks, you can automatically change the Subscription post status back to Pending or Draft, which will remove it from the Locations View on the Business page. It will be replaced by the "Purchase Subscription" link. If you are able to find the appropriate hook, I can help you implement the status change in PHP, but I'm not really qualified to help you find the right hook or to know how WC Subscriptions works.
Let me know if this is getting you in the right direction, or if you have additional questions.
Hi Christian,
this sounds really good so far thanks ????
Right now I dont quite understand how to go about point 2 ("2. When a user registers, automatically create a single Business for each User and make that User the author of the Business.")
How can I create a user registration form that automatically creates a Business post? Also while we are at the registration.. Is there any way to include a set of checkboxes within the registration form and depending on the selection, a different user role is assigned to that user?
Right now I dont quite understand how to go about point 2
Okay it depends on how registration works. I assume the registration form is a new User CRED Form, correct? If so, you can use the cred_save_data hook in PHP to fire some action when the form is submitted successfully. This action will include some code that creates a post automatically using the WordPress wp_insert_post() method.
The cred_save_data hook is described in detail here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
Example below.
The wp_insert_post method is described in detail here:
https://developer.wordpress.org/reference/functions/wp_insert_post/
Example:
// Create post object
$my_post = array(
'post_title' => 'Business Name',
'post_content' => 'Business post content',
'post_status' => 'publish',
'post_author' => 123
'post_type' => 'business'
);
wp_insert_post( $my_post );
We're available to give more guidance on these functions when you're ready to implement them. Create new tickets for each issue to receive the best support.
Is there any way to include a set of checkboxes within the registration form and depending on the selection, a different user role is assigned to that user?
Yes, this is possible. The general idea is that you create some custom role like "pending". Your CRED form will be set to create Users in this role by default. You will include a generic field in your CRED form that includes the different role options, and use the same cred_save_data hook to change the User's role based on the selected option. Here's some sample code that shows how this works:
add_action('cred_save_data', 'cred_update_user_role_action',10,2);
function cred_update_user_role_action($user_id, $form_data) {
if ($form_data['id'] == 12345)
{
// modify the user role to match the selected option
$role = $_REQUEST['user_select_role'];
$u = new WP_User( $user_id );
$u->remove_role( 'pending' ); // cred form role setting
$u->add_role( $role ); // user-selected role
}
}
You would have to modify the 12345 to match your CRED form, the 'user_select_role' to match your generic field, and 'pending' to match the slug of your temporary User role.
Hi Christian,
thanks a lot! ???? Should I close this thread then and open a new one? if it's ok to continue here since you seem to know everything I need let me know ????
I tried implementing your code to create a post on user registration.. It does work but it creates a lot of "business" posts and when I delete them, new ones appear as if its hung up in a loop or something
I used your exact code except for replacing the name of the post type like so:
// Create post object
$my_post = array(
'post_title' => 'Business Name',
'post_content' => 'Business post content',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'firma'
);
wp_insert_post( $my_post );
Also.. how can I set 'Business Name" and the content to be taken from a custom user field created with types?
The new post code I showed was just an example, and will not work for your needs verbatim. It needs to be applied within a cred_save_data hook, otherwise the code will fire over and over whenever a page is loaded, like you are describing. You should remove the code from the functions.php file for now, and open a new ticket to get some help creating a post automatically when a new User is created using CRED. I'll be glad to get that working for you. If you would like to assign the ticket to me directly, follow these steps:
1. Mark this ticket as resolved.
2. Use the support ranking system to rate my support. If you choose the highest ranking, you will be given the option to assign the next ticket you create directly to me. If you choose a different ranking that's fine too, but the next ticket you create will go into the support queue and may be addressed by another supporter.
3. Create the new ticket.
Ah thanks a lot 🙂 actually just stumbled upon an older thread of yours (https://toolset.com/forums/topic/auto-create-custom-post-after-new-user-submission/) so gonna give that a try first before opening a new ticket.
Thats a neat trick didnt know that and even if it wasnt for that, your support is great so highest rating it is 🙂