Skip Navigation

[Resolved] CRED form with select woocommerce order

This support ticket is created 4 years, 11 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.

Our next available supporter will start replying to tickets in about 8.89 hours from now. Thank you for your understanding.

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: Asia/Karachi (GMT+05:00)

Tagged: 

This topic contains 10 replies, has 3 voices.

Last updated by Waqar 4 years, 10 months ago.

Assisted by: Waqar.

Author
Posts
#1410757

I'm Not sure if I can do this with cred forms, but I need a way to select an order from my woocommerce orders (filtered by a field in the order) and edit a field in that specific order. I've looked around a bit, but not found a way to do it yet....So as an example. I have orders, such as (order1 (john smith), order2 (bob smith),order3(joe smith)) etc. I would like to show a select list that lists the billing_name on that order so that someone can select that order by billiing_name and then edit a field on that order. I'm wondering if this is possible, perhaps pointing me to some documentation that could help me get started.

Basically I have a custom field in my orders (called "matched to a roomate") . they are defaulted to "No" when the order is placed. I need to create a form that can display all the orders that have that field set to "NO" in a select list, that a user can select an order (by billing name) and then have that form submitted and change that field to "Yes" via the cred form.

Thanks much++ . any help would be much appreciated++

#1411753

Nigel
Supporter

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

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

Hi Charlene

Without going into too much details, it seems like your proposal is slightly misconceived, which is probably why you are having problems implementing it.

You don't create lists of posts with Forms.

You create lists of posts with Views.

It looks to me like you need to create a View that displays orders that includes a Query Filter for your "matched to a roomate" custom field to only display posts where that is "NO".

In the output section list whatever fields you need to display, *and* a link to edit that order which will take you to a form where the field could be updated.

Actually it might be easier to make a very simplified version of the edit form that includes only the "matched to a roomate" field and a submit button which is inserted directly into the View output so that the updates to the field could be made from the same page.

I think that would be the angle to approach this from.

If you have problems implementing it, let me know.

#1414403

Thanks much,
I had not actually started implementing it yet, as I was uncertain where to start. I understand working with lists created by views and have done this many times. I just wasn't sure how to wrap my head around this use case scenario, but your description helped.

I was able to implement a solution using each method you described above, and they both work great.

Not quite as elegant as I want, although they both work, The first one requires a link to another page, and the second has a submit button for each loop.

As stated both solutions work for me, so I could use either solution, but I'm wondering if there is a way to generate the list of filtered orders in a view, and insert that view into a cred form so that there would only be one submit button. Not sure if I'm explaining that well enough, but it would look something like this:

Field one: A select field that would contain the orders to choose from (by billing name)
Field two: A hidden field that would be for the roomate_matched field
Field three: A submit button.

This way a user could view the page(form) and select the name from the dropdown select, and hitting submit would edit the field in the order selected by the user. It would be a more elegant solution with just a dropdown list and a single submit button. No worries if not, as either solution you suggested will work, but if I can make it simpler that would be helpful.

I'm guessing that if it was possible, I'd need to create a view that contains the billing_name of the order along with the order ID that would somehow be inserted into the cred form and have that cred form be able to populate the dropdown with the billing_name, a hidden field with the "roomate_matched" set to "yes" and then have the submit button edit the order associated with whatever billing_name was selected.

Once again, no worries if not possible, but if it is I'd like to try and accomplish it.

Thanks again++

#1414865

Hi Charlene,

Thanks for writing back and for sharing further details.

Nigel has a day off today, so I'll be following up on this ticket.

What you're planning is possible, but will require some extra steps:

1. A "select" type generic field can be used in a Toolset Form, to dynamically populate the options using a Toolset post view.
( ref: https://toolset.com/documentation/user-guides/front-end-forms/inserting-generic-fields-into-forms/ )

Example:


[cred_generic_field type='select' field='order-post-select']
{
"required":1,
"options":[ [wpv-view name="slug-of-the-view"]  ]
}
[/cred_generic_field]

Note: You'll replace "slug-of-the-view" with the actual slug of the view explained in the next step.

2. This generic field will expect the view's output in this format:


{"value": "value1", "label": "Label 1"}, {"value": "value2", "label": "Label 2"}

You'll create a new post view for your desired post type with the necessary query filters ( as Nigel explained), but you'll select the "List with seperators" option in the "Loop Wizard" to get the raw/unformatted output from it.

Screenshot: hidden link

In the "Loop Editor", you can adjust the content, so that the view returns the output in the required format using post IDs as option values and the post titles as option labels.


[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
			[wpv-item index=other]
				{"value": "[wpv-post-id]", "label": "[wpv-post-title]"},
			[wpv-item index=last]
				{"value": "[wpv-post-id]", "label": "[wpv-post-title]"}
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		{"value": "", "label": "No items found"}
	[/wpv-no-items-found]
[wpv-layout-end]

3. Next, you'll need a form in which you can use this generic field. Since through this form, you don't actually want to create or edit a particular post, you can create a new "edit post" form for any post type which doesn't have any required fields.

Make sure that the form only includes the "form_messages" and "form_submit" fields and then insert this select type generic field and the hidden type generic field for the custom field key ( roomate_matched ) and its value ( yes ).

As a result, only the post-selection field and submit button will show on the front-end.

This form can be inserted on any page and for the target post, you can select any post.
(it doesn't matter because no changes will be made to the target post)

4. The last step would be updating the custom field value based on the user's selected post, on the form's submission. For this, you can use "cred_save_data" hook:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1415183

Looks like what I want to do. I"m having difficult getting the select list to show up in the form.

Here is my view:

[wpv-layout-start]

	[wpv-items-found]

	<!-- wpv-loop-start -->

		<wpv-loop>

			[wpv-item index=other]

				{"value": "[wpv-post-id]", "label": "[wpv-post-field name="_billing_last_name"]"},

			[wpv-item index=last]

				{"value": "[wpv-post-id]", "label": "[wpv-post-field name="_billing_last_name"]"}

		</wpv-loop>

	<!-- wpv-loop-end -->

	[/wpv-items-found]

	[wpv-no-items-found]

		{"value": "", "label": "No items found"}

	[/wpv-no-items-found]

[wpv-layout-end]

and it's output:

{"value": "33398", "label": "Dude"}, {"value": "33389", "label": "Person"}, {"value": "33382", "label": "subject"}

here is the cred form:

[credform]
	[cred_generic_field type='select' field='order-post-select']
{
"required":0,
"options":[ [wpv-view name="matched"] ]
}
[/cred_generic_field]
	<div class="form-group">
		<label>shared_room_matched</label>
		[cred_field field='shared_room_matched' force_type='field' class='form-control' output='bootstrap']
	</div>
	[cred_field field='form_submit' output='bootstrap' value='Submit' class='btn btn-primary btn-lg']
[/credform]

Here is also my code for the submit:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==33427)
    {
        if (isset($_POST['shared_room_matched']))
        {
            // add it to saved post meta
            add_post_meta($post_id, '__shared_room_matched', $_POST['shared_room_matched'], true);
        }
    }
}

All seems to look ok, but still no select list :-/ what might i be missing?
thanks much+++

#1415289

I found in another post the need to add this to functions.php to make it work, for clean output of the view:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
    
function prefix_clean_view_output( $out, $id ) {
    if ( $id == '999' ) { //Please adjust to your Views ID
        $start = strpos( $out, '<!-- wpv-loop-start -->' );
        if ( 
            $start !== false
            && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
        ) {
            $start = $start + strlen( '<!-- wpv-loop-start -->' );
            $out = substr( $out , $start );
            $end = strrpos( $out, '<!-- wpv-loop-end -->' );
            $out = substr( $out, 0, $end );
        }
    }
    return $out;
}

it works, but please let me know if this is still the correct way to fix the issue.

thanks much++_

#1416161

Thank you for sharing the update.

To troubleshoot why the options list is not showing, I'll need access to your website's admin area.

Can you please share temporary admin login details, along with the link to the page with this form?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

And the code snippet from your second message is now, no longer needed with the "List with seperators" option in the view.

#1417545

Thank you for sharing the admin access details, but I'm getting the following message:

"Unknown username. Check again or try your email address."

I've set your next reply as private again so that you can test and share the updated details.

#1421231

Thank you for the updated admin access details and here any my findings:

1. I inserted your view and form on a test page and they both seem to be working as expected.
yourwebsite.com/wp-admin/post.php?post=33731&action=edit&classic-editor
( yourwebsite.com/?page_id=33731&preview=true )

Note: Since you've set the form to edit "order" type posts, you'll need to specify the ID of any order post in the shortcode through the "post" attribute.
( but please make sure that this order is never removed/deleted )

2. The form is calling the value for a custom field "shared_room_matched" in the hidden field which will always get the value saved with the order post whose ID is used in the form's shortcode.
( in this example page, the ID used is "33722" ).

Since there are only two possible values involved which are 'yes' or 'no', you don't need that hidden field in the form and it can be removed.

3. The code snippet for the submit action through "cred_save_data" hook is not correct. It is adding a new custom field value using "$post_id" which will have the ID of the post which is being edited through the form.
( in this example page, that ID is "33722" and as a result, the code will always add custom field value to that specific post )

We want to update the custom field value for the order post which is selected by the visitor, which is why the code will look like this:


add_action('cred_save_data', 'this_save_data_action',10,2);
function this_save_data_action($post_id, $form_data)
{
	// if a specific form
	if ($form_data['id']==33427)
	{
		if (isset($_POST['order-post-select']))
		{
			update_post_meta( $_POST['order-post-select'], 'shared_room_matched', 'yes', 'no' );
		}
	}
}

The above snippet will only execute for the form with ID "33427" and if a generic field "order-post-select" value is available, it will update the custom field value for a field with key "shared_room_matched" where the old value is "no" and the new will value will be "yes", for that selected order post.

I hope this makes it more clear.

#1426811

Thanks much, I’ve not had a chance to look at this, as I’m on vacation till the 12th of January. I’m writing now because I got a Bot alert that the post would be closed. Hoping I can leave it set to open till my return when I have time to look at the issue. Thanks

#1427515

No worries and posts can be kept open usually for a couple of weeks.

If you expect to return after a longer period, you can always mark this ticket as resolved and for any follow-up questions, you can start a new one.
( just mention that it is related to another ticket and include this ticket's link )

Happy Holidays and a wonderful New Year!