Skip Navigation

[Resolved] filter orders in view

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.

This topic contains 6 replies, has 2 voices.

Last updated by Nigel 2 years, 9 months ago.

Assigned support staff: Nigel.

Author
Posts
#1167515

Hi, I created a view with all woocommerce order.

I need to filter this order in base of category of the product bought. How can I do?

#1167672

Nigel
Supporter

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

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

Hi there

That's not possible. It wouldn't be possible if you were writing your own custom WP_Queries by hand, and so it is not possible with Views.

You can only filter posts by properties of those posts.

Product categories belong to products. They are not a property of orders.

You can only filter orders by fields or taxonomies of orders.

What you are aiming for would require a custom solution. You could try consulting the Toolset Contractors page for someone to develop this: https://toolset.com/contractors/

#1167892

If I set a relationship between Product and Orders, could I get category rom the parent (product) and use it as filter?
What do you think? Should be possible?

#1167907

OK Nigel, I find a possible solution. I set a relationship between orders and product.
So in the view I can Get the category of the product as parent, and use it as filter. It works.

I only need your help to hiw to set this relationshio automatically. When a new order is created ,should be automatically set a relationship with the product of the order. Please , can you help me in this issue?

#1167926

With this code I set automatically the relationship.

// Set product-order relationship 
add_action( 'woocommerce_thankyou', 'relate_product_order_func', 999,2 );
function relate_product_order_func( $order_id ){
	
	$order = wc_get_order( $order_id );
	foreach($order->get_items() as $order_key => $order_value)
        {
	     $product_id = $order_value->get_data()['product_id'];
        }   
	
	toolset_connect_posts('prodotto-prenotazione',$product_id, $order_id);
	
}
#1167931

Hi Nigel,

Now for each order I've the relative product category.

How can I filter the view in base of the category of the parent?

#1168538

Nigel
Supporter

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

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

Sorry, but filtering by fields (or taxonomies) of related posts isn't yet supported. The intention is to add such support, but among the competing demands on the developers' time it hasn't yet made it to the front.

(You can push for it to be developed here: https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/.)

In the meantime you would need to duplicate the product category taxonomy on orders to be able to filter orders by product category. So you could modify the code you use above to connect an order with a product to instead get the product category from the product and apply it to the order post.

You would then be able to filter orders by product category.