Skip Navigation

[Resolved] Filter view by woocommerce extra field

This support ticket is created 5 years, 9 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by domenicoS 5 years, 8 months ago.

Assisted by: Nigel.

Author
Posts
#1202144

Hi, I created a view of woocommerce orders. The view works good.
I need to filter this order in base of a woocommerce extra field I added.

This is the code used to add the field

//-----Add Fields "Seconda Persona"-----//

//Add Fields
add_action( 'woocommerce_after_checkout_billing_form', 'add_second_person_field' );
function add_second_person_field( $checkout ){
 
	// you can also add some custom HTML here
 
	woocommerce_form_field( 'second-person', array(
		'type'          => 'text', 
		'required'	=> false, 
		'label'         => '2 person,
			'By phone'	=> 'By phone', // 'value'=>'Name'
			'By email'	=> 'By email'
			)
		), $checkout->get_value( 'second-person' ) );
 
	// you can also add some custom HTML here
 
}


 // Save added Fields
add_action( 'woocommerce_checkout_update_order_meta', 'save_added_fields' );
function save_added_fields( $order_id ){ 
	if( !empty( $_POST['second-person'] ) )
		update_post_meta( $order_id, 'second-person', sanitize_text_field( $_POST['second-person'] ) );
	if( !empty( $_POST['dog-field'] ) )
		update_post_meta( $order_id, 'dog-field', sanitize_text_field( $_POST['dog-field'] ) );
	
}
// Display User Field @ Order Meta
 
add_action( 'woocommerce_admin_order_data_after_billing_address', 'bbloomer_checkout_field_display_admin_order_meta', 10, 1 );
 
function bbloomer_checkout_field_display_admin_order_meta( $order ) {    
echo '<p><strong>'.__('Secondo partecipante').':</strong><br/> ' . get_post_meta( $order->id, 'second-person', true ) . '</p>';
}

How can I filter the view in base of this field (diasplay if has a value and not empty)?

Thank you for your help.

Domenico

#1202433

Nigel
Supporter

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

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

Hi Domenico

It looks like your order posts will have an additional custom field with a slug of 'second-person'. It's not a Types field, but if you insert a Views filter it should still find this custom field if you already have some order posts that have a value for this field, and you can simply create a filter as normal, just like it was a Types field.

Did you try already and have problems?

#1202552

Hi Nigel,
Yes it is a field.
I try to search the slug in the filter suggested, but I did not find my custom fields.
The fields are 3:
- second-person
- dog-field
- affiliation-field

The one interestig me is the third "affiliation-field". I need to filter the view with only order have a value in this field, but as I said, i didn't find thi slug in the filters.

At the moment I used a conditional output to display only orders with a value of affiliation-code different then 0.

But I ask you if is possible use a filter and not a conditional output.

#1203333

Nigel
Supporter

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

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

Screenshot 2019-02-20 at 17.35.13.png
Screenshot 2019-02-20 at 17.33.42.png

Hi Domenico

I just checked this on a local test site.

See the two screenshots.

I edited an existing product and made up a custom field that I added to that one product – a custom field with a slug of "nigel". (I used the Screen Options tab at the top of the edit page to expose the standard custom fields metabox to add this custom field as a simpler alternative than directly adding the field to the database.)

So now I have one product which has a custom field with a slug of "nigel".

I then created a View and inserted a Query Filter. You can see in the second screenshot that the custom field was immediately available, Views recognised that a product has a 'nigel' custom field.

If you do not see your custom fields available then it likely means that the data is not saved correctly, that if you check in the database you will find that there are no products that have postmeta with keys second-person, dog-field, or affiliation-field.

#1213602

My issue is resolved now. Thank you!