[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".
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.
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.
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/