Only query order meta for orders that were successfully retrieved from the DB. (#45544)

* Only query order meta for orders that were successfully retrieved from the DB.

* Add changefile(s) from automation for the following project(s): woocommerce

* Address linting issue

---------

Co-authored-by: github-actions <github-actions@github.com>
Co-authored-by: Jorge Torres <jorge.torres@automattic.com>
This commit is contained in:
Michael Pretty 2024-04-15 16:37:47 -04:00 committed by GitHub
parent a8e84d8c8b
commit aa52be1ee7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 16 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Only query order meta for orders that were successfully retrieved from the DB

View File

@ -1631,15 +1631,7 @@ WHERE
);
// phpcs:enable
$meta_data_query = $this->get_order_meta_select_statement();
$order_data = array();
$meta_data = $wpdb->get_results(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $meta_data_query and $order_meta_table is autogenerated and should already be prepared. $id_placeholder is already prepared.
"$meta_data_query WHERE $order_meta_table.order_id in ( $id_placeholder )",
$ids
)
);
$order_data = array();
foreach ( $table_data as $table_datum ) {
$id = $table_datum->{"{$order_table_alias}_id"};
@ -1663,14 +1655,27 @@ WHERE
$order_data[ $id ]->meta_data = array();
}
foreach ( $meta_data as $meta_datum ) {
// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Not a meta query.
$order_data[ $meta_datum->order_id ]->meta_data[] = (object) array(
'meta_id' => $meta_datum->id,
'meta_key' => $meta_datum->meta_key,
'meta_value' => $meta_datum->meta_value,
if ( count( $order_data ) > 0 ) {
$meta_order_ids = array_keys( $order_data );
$meta_order_id_placeholder = implode( ', ', array_fill( 0, count( $meta_order_ids ), '%d' ) );
$meta_data_query = $this->get_order_meta_select_statement();
$meta_data = $wpdb->get_results(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $meta_data_query and $order_meta_table is autogenerated and should already be prepared. $id_placeholder is already prepared.
"$meta_data_query WHERE $order_meta_table.order_id in ( $meta_order_id_placeholder )",
$ids
)
);
// phpcs:enable
foreach ( $meta_data as $meta_datum ) {
// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Not a meta query.
$order_data[ $meta_datum->order_id ]->meta_data[] = (object) array(
'meta_id' => $meta_datum->id,
'meta_key' => $meta_datum->meta_key,
'meta_value' => $meta_datum->meta_value,
);
// phpcs:enable
}
}
return $order_data;
}