Skip Navigation

[Resolved] Filter WooCommerce orders by the product that was ordered

This thread is resolved. Here is a description of the problem and solution.

Problem:

The user asked if it is possible to join WooCommerce products and orders in post-relationship so that ordered posts can be filtered by the products in a view.

Solution:

Informed that this is possible using custom code and shared information about the WooCommerce's hook "woocommerce_order_status_completed" and Toolset function "toolset_connect_posts".

Relevant Documentation:

https://squelchdesign.com/web-design-newbury/woocommerce-detecting-order-complete-on-order-completion/

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

This support ticket is created 2 years, 5 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 2.24 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)

Author
Posts
#2226575

Hi,

I have a small WooCommerce store where people can order tickets to participate in a competition. For each competition, I want to create a page with a list of participants. I'm doing this by creating a view that displays WooCommerce orders in front-end pages. Is it possible to filter this query based on the product that was ordered?

I'm currently working around this by adding a custom checkbox group field to the Order post type, and then manually going through each order to assign them to the correct competition(s).

I also tried to create a many-to-many relationship between Order and Product, but this did not work for existing orders.

Best regards,
Guus Frenken

#2226833

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Welcome to Toolset support and I'd be happy to assist.

Since there is no built-in query filter available to filter the WooCommerce orders by the product, it will require some code customization.

Your approach of using a many-to-many relationship seems like a good option to join 'Orders' and "Products" posts, in a way that can be used for filtering the results in views.

a). For new orders:

You can use WooCommerce's hook "woocommerce_order_status_completed" ( ref: hidden link ) to execute a custom function that can get all the "products" from an "order" when it is completed, and then connect each product to the order, in the many-to-many relationship, using the "toolset_connect_posts" function.
( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts )

As a result, whenever a new order will be completed, a relationship connection will be created automatically between that "Order" post and the "Product" posts included in that order.

b). For existing order:

For "Orders" which are already completed (existing orders), you'll need another custom function to run one time, that can loop through all the order posts, get the IDs of the products included in an order and then connect them with that order post, in a relationship connection.

I hope this helps and for more personalized assistance around custom code, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#2227933

Hi Waqar,

Thank you for the help, I was able to figure it out using your advice.

Here is my code in case anyone runs into a similar issue. This code assumes your many-to-many relationship's slug is order-product

Quick and dirty one-time-use code to connect existing orders:

$loop = new WP_Query( array(
	'post_type' => 'shop_order',
	'post_status' => array_keys( wc_get_order_statuses() ),
	'posts_per_page' => -1,
) );

$orders = $loop->posts;

foreach ( $orders as $order ) {
	$order = wc_get_order( $order->ID );

	foreach ( $order->get_items() as $item_id => $item ) {
		toolset_connect_posts( 'order-product', $order_id, $item->get_product_id() );
	}
}

Connect new orders using woocommerce_checkout_order_processed hook:

add_action( 'woocommerce_checkout_order_processed', 'connectOrder', 1, 1 );

function connectOrder( $order_id ) {
    $order = wc_get_order( $order_id );

    foreach ( $order->get_items() as $item_id => $item ) {
        toolset_connect_posts( 'order-product', $order_id, $item->get_product_id() );
    }
}
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.