Skip Navigation

[Resolved] How to get all the orders that are made for a particular product

This support ticket is created 5 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.

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)

This topic contains 3 replies, has 2 voices.

Last updated by puneetS-3 5 years, 5 months ago.

Assisted by: Waqar.

Author
Posts
#1312875

I would like to show all the orders that is made for that product in a list, also I would like to show a detailed view of the order.

I have users who create products and would like to show there sales. What filters do I need to use to show all the orders for that product that he has created.

#1313091

Hey Waqar, Anything on this?

#1313197

Hi Puneet,

Thank you for waiting.

I've performed some testing and research and here are my findings:

1. Using Toolset Views, you can create a post view that is set to show "Products" post type. To show only the products from a specific user, you can add a post author filter and select the option, based on how you're planning to use this view.
( screenshot: hidden link )

2. To show only orders from a specific product, there is no built-in filter available, but you'll need a custom shortcode that can use WooCommerce's own functions to check whether the current product exists or not in order.
( ref: https://wordpress.stackexchange.com/a/97228 )

For example, you can add the following code for a custom shortcode in your active theme's "functions.php" file:


add_shortcode( 'check_for_product_in_order', 'check_for_product_in_order_func');
function check_for_product_in_order_func( $atts ) {
	$atts = shortcode_atts( array(
		'product' => '',
		'order' => '', 
	), $atts );

	$order = wc_get_order( $atts['order'] );
	$items = $order->get_items();

	$found_items[] = '';

	foreach ( $items as $item ) {
		$product_name = $item->get_name();
		$product_id = $item->get_product_id();

		if($product_id == $atts['product']) {
			$found_items[] = $product_id;
		}
	}

	if(count($found_items) > 1) {
		return 1;
	}
	else
	{
		return 0;
	}
}

This shortcode will accept two parameters, "product" (id) and "order" (id) and will return 1 if the product is found in the order and 0 if it doesn't.

Important note: Please add "check_for_product_in_order" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

3. You'll create a new post view that will show "Orders" post type. It will show all the orders by default, but to limit its display only to orders which have a specific product in them, you can use the new custom shortcode in a conditional block so that it only displays the result, when the shortcode returns 1.
( ref: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/ )

You can wrap the loop item content of this view in a condition like this:


[wpv-conditional if="( '[check_for_product_in_order product="[wpv-attribute name="product"]" order="[wpv-post-id]"]' eq 1 )"] 
Order Title: [wpv-post-title]<br>
Order ID: [wpv-post-id]<br>
[/wpv-conditional]

4. In the last step, you can place the shortcode for this order view, in the loop of the products view (created in step 1), like this:


[wpv-view name="view-to-show-orders-for-a-specific-product" product="[wpv-post-id]"]

Feel free to replace the slug of the view as per your website and note that we're passing the current product's ID to the nested orders view because we need it for the comparison in the conditional code block with the custom shortcode.
( ref: https://toolset.com/documentation/user-guides/passing-arguments-to-views/ )

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/

For assistance related to WooCommerce plugin's features and functions, you can consult its official support and documentation.

regards,
Waqar

#1313609

Wow, this has been of a great help. Thanks a lot!