prefix}wc_order_product_lookup opl JOIN {$wpdb->prefix}wc_order_stats os ON os.order_id = opl.order_id LEFT JOIN {$wpdb->postmeta} pm ON pm.post_id = COALESCE(NULLIF(opl.variation_id, 0), opl.product_id) AND pm.meta_key = '_price' JOIN {$wpdb->term_relationships} tr ON tr.object_id = opl.product_id JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id = tr.term_taxonomy_id AND tt.taxonomy = 'product_cat' JOIN {$wpdb->terms} t ON t.term_id = tt.term_id $where GROUP BY t.term_id ORDER BY revenue DESC"; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $results = $wpdb->get_results( $query, ARRAY_A ); $labels = array(); $revenue = array(); $quantity = array(); foreach ( $results as $row ) { $labels[] = $row['name']; $revenue[] = (float) $row['revenue']; $quantity[] = (int) $row['items_sold']; } return array( 'table' => $results, 'chart' => array( 'labels' => $labels, 'revenue' => $revenue, 'quantity' => $quantity, ) ); } }