Tell us what you are trying to do?
Perform calculations on view data and then present it on the front end as summaries.
Is there any documentation that you are following?
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results
I have working code that will take a View's output and then allow me to calculate summaries and totals based on the data in the View, which can then be displayed via shortcode. This works perfectly and the code is able to handle different views of the same type so I can adjust what is being displayed.
The issue I've run into is the get_view_query_results function doesn't take the fact that the second View has a filtered list of post ID's being passed to it via shortcode into consideration. Now this makes sense as it's being passed like this:
[wpv-view name='summary-of-enrolment-report' ids='[non_deposit_orders]']
And I'm sure I need to pass this list via the $args parameter of get_view_query_results. But nothing I do seems to work and the documentation is uselessly vague on how this actually works, simply saying "Optional array of attributes to pass to the View, like shortcode attributes when using the wpv-view shortcode."
Here is the main code:
function get_report_totals( $atts ) {
//Set the default $atts values
$defaults = array(
'view' => 3112, //Accounts Receivable View
'summary' => 'paid',
'output' => 'total'
);
//Apply default atts if none have been set
$atts = shortcode_atts( $defaults, $atts );
//Get the results of the View query object
$posts = get_view_query_results( $atts['view'] );
//Set an initial counter variable
$count = 0;
switch( $atts['summary'] ){
case 'paid':
$status = array( 'wc-completed' );
break;
case 'outstanding':
$status = array( 'wc-pending', 'wc-deposit-paid' );
break;
case 'partial':
$status = array( 'wc-deposit-paid' );
break;
case 'unpaid':
$status = array( 'wc-pending' );
break;
default:
$status = array( 'wc-completed' );
}
foreach( $posts as $post ){
if( in_array( $post->post_status, $status ) ){
if( $atts['output'] == 'total' ){
//Get the WC_Order object for the current order in the loop
$order = wc_get_order( $post->ID );
//Add order total to the running total
$count = $count + $order->get_total();
} elseif( $atts['output'] == 'count' ){
$count = $count + 1;
}
}
}
return $count;
}
add_shortcode( 'report_totals', 'get_report_totals' );
I've tried modifying the View call based on this support thread:
https://toolset.com/forums/topic/big-problem-with-view-2/#post-1227849
Like so:
$args = array(
'ids' => '[non-deposit-orders]'
);
//Get the results of the View query object
$posts = get_view_query_results( $atts['view'], NULL, NULL, $args );
But this returns 0 every time. Without this post ID filter (which returns a comma-separated list), the calculations are run on every order on the system, not just the relevant ones. But I can't see how I'm expected to pass this to get_view_query_results.
Could someone please clarify how this is intended to work?