Skip Navigation

[Resolved] Show orders by product author

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

Problem:

The issue here is that the user wanted to filter the list of orders by the current product author. This means that if an author views his profile then he can view all the orders that were made on his product.

Solution:
I've made a custom shortcode that should be able to get the product id of the current order and pass it to your list orders view.

Add the following to your functions.php file

// Add Shortcode
function get_ordered_products_id( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'order_id' => '',
        ),
        $atts
    );
 
    $order = wc_get_order( $atts['order_id'] ); 
    $product_ids = array( );;
     
    // Iterating through each "line" items in the order
    foreach ($order->get_items() as $item_id => $item_data) {
     
        // Get an instance of corresponding the WC_Product object
        $product = $item_data->get_product();
        array_push($product_ids, $product->get_ID()); // Get the product name
    }
 
    //$new_product_ids = str_replace(" ",",",$product_ids);
 
    return implode(',',$product_ids);
 
}
add_shortcode( 'get_ordered_products_id', 'get_ordered_products_id' );

This shortcode is used by calling it like this [get_ordered_products_id order_id='192'] where 192 will be the order id. You can also use it with the views [wpv-post-id] shortcode by passing it into the shortcode like this [get_ordered_products_id order_id='[wpv-post-id]']

So to use this in the view you need to add "get_ordered_products_id" shortcode name to the views 3rd party shortcode arguements that can be found in Toolset->Settings->Frontend Content.

So this should be added to your Orders view inside the tags. The only thing that should be within those tags is this.

[wpv-view name="list-of-ordered-products" ids="[get_ordered_products_id order_id='[wpv-post-id]']"]
This support ticket is created 6 years, 7 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 16 replies, has 3 voices.

Last updated by Shane 6 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#716257

Hi Shane,
Any help with this? It's been a while...

#728695

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nuno,

I must apologize for not providing an update on this one.

I've made a custom shortcode that should be able to get the product id of the current order and pass it to your list orders view.

Add the following to your functions.php file

// Add Shortcode
function get_ordered_products_id( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'order_id' => '',
		),
		$atts
	);

	$order = wc_get_order( $atts['order_id'] ); 
    $product_ids = array( );;
	
	// Iterating through each "line" items in the order
	foreach ($order->get_items() as $item_id => $item_data) {
	
	    // Get an instance of corresponding the WC_Product object
	    $product = $item_data->get_product();
	    array_push($product_ids, $product->get_ID()); // Get the product name
	}

    //$new_product_ids = str_replace(" ",",",$product_ids);

    return implode(',',$product_ids);

}
add_shortcode( 'get_ordered_products_id', 'get_ordered_products_id' );

This shortcode is used by calling it like this [get_ordered_products_id order_id='192'] where 192 will be the order id. You can also use it with the views [wpv-post-id] shortcode by passing it into the shortcode like this [get_ordered_products_id order_id='[wpv-post-id]']

So to use this in the view you need to add "get_ordered_products_id" shortcode name to the views 3rd party shortcode arguements that can be found in Toolset->Settings->Frontend Content.

So this should be added to your Orders view inside the <wpv-loop> tags. The only thing that should be within those tags is this.

      	[wpv-view name="list-of-ordered-products" ids="[get_ordered_products_id order_id='[wpv-post-id]']"]

Please try this and let me know if this helps.
Thanks,
Shane