From b42ec5f4769610a5d1da14ca60a9697f3d90fd9c Mon Sep 17 00:00:00 2001 From: Raluca Stan Date: Wed, 29 Dec 2021 15:15:52 +0100 Subject: [PATCH 1/4] Account for products without variations in the prime_product_variations function --- src/BlockTypes/AbstractProductGrid.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/BlockTypes/AbstractProductGrid.php b/src/BlockTypes/AbstractProductGrid.php index aa441c94f5b..349717923f8 100644 --- a/src/BlockTypes/AbstractProductGrid.php +++ b/src/BlockTypes/AbstractProductGrid.php @@ -361,14 +361,17 @@ protected function prime_product_variations( $product_ids ) { $product_variations = $wpdb->get_results( "SELECT ID as variation_id, post_parent as product_id from {$wpdb->posts} WHERE post_parent IN ( " . implode( ',', $prime_product_ids ) . ' )', ARRAY_A ); $prime_variation_ids = array_column( $product_variations, 'variation_id' ); $variation_ids_by_parent = array_column( $product_variations, 'product_id', 'variation_id' ); - $all_variation_meta_data = $wpdb->get_results( - $wpdb->prepare( - "SELECT post_id as variation_id, meta_key as attribute_key, meta_value as attribute_value FROM {$wpdb->postmeta} WHERE post_id IN (" . implode( ',', array_map( 'esc_sql', $prime_variation_ids ) ) . ') AND meta_key LIKE %s', - $wpdb->esc_like( 'attribute_' ) . '%' - ) - ); - // phpcs:enable + $all_variation_meta_data = []; + if ( ! empty( $prime_variation_ids ) ) { + $all_variation_meta_data = $wpdb->get_results( + $wpdb->prepare( + "SELECT post_id as variation_id, meta_key as attribute_key, meta_value as attribute_value FROM {$wpdb->postmeta} WHERE post_id IN (" . implode( ',', array_map( 'esc_sql', $prime_variation_ids ) ) . ') AND meta_key LIKE %s', + $wpdb->esc_like( 'attribute_' ) . '%' + ) + ); + } + // phpcs:enable // Prepare the data to cache by indexing by the parent product. $primed_data = array_reduce( $all_variation_meta_data, From 3452b7bf25572ac11b8a9ac721dd4f1c0a4545e7 Mon Sep 17 00:00:00 2001 From: Raluca Stan Date: Thu, 30 Dec 2021 11:05:40 +0100 Subject: [PATCH 2/4] Update src/BlockTypes/AbstractProductGrid.php Co-authored-by: Mike Jolley --- src/BlockTypes/AbstractProductGrid.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/BlockTypes/AbstractProductGrid.php b/src/BlockTypes/AbstractProductGrid.php index 349717923f8..a20b7a70570 100644 --- a/src/BlockTypes/AbstractProductGrid.php +++ b/src/BlockTypes/AbstractProductGrid.php @@ -363,14 +363,16 @@ protected function prime_product_variations( $product_ids ) { $variation_ids_by_parent = array_column( $product_variations, 'product_id', 'variation_id' ); $all_variation_meta_data = []; - if ( ! empty( $prime_variation_ids ) ) { - $all_variation_meta_data = $wpdb->get_results( - $wpdb->prepare( - "SELECT post_id as variation_id, meta_key as attribute_key, meta_value as attribute_value FROM {$wpdb->postmeta} WHERE post_id IN (" . implode( ',', array_map( 'esc_sql', $prime_variation_ids ) ) . ') AND meta_key LIKE %s', - $wpdb->esc_like( 'attribute_' ) . '%' - ) - ); + if ( empty( $prime_variation_ids ) ) { + return; } + + $all_variation_meta_data = $wpdb->get_results( + $wpdb->prepare( + "SELECT post_id as variation_id, meta_key as attribute_key, meta_value as attribute_value FROM {$wpdb->postmeta} WHERE post_id IN (" . implode( ',', array_map( 'esc_sql', $prime_variation_ids ) ) . ') AND meta_key LIKE %s', + $wpdb->esc_like( 'attribute_' ) . '%' + ) + ); // phpcs:enable // Prepare the data to cache by indexing by the parent product. $primed_data = array_reduce( From f2662c12ca57f521286d8fdb52837918cb26bb5a Mon Sep 17 00:00:00 2001 From: Raluca Stan Date: Thu, 30 Dec 2021 11:21:14 +0100 Subject: [PATCH 3/4] Fix whitespace --- src/BlockTypes/AbstractProductGrid.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BlockTypes/AbstractProductGrid.php b/src/BlockTypes/AbstractProductGrid.php index a20b7a70570..4b567756905 100644 --- a/src/BlockTypes/AbstractProductGrid.php +++ b/src/BlockTypes/AbstractProductGrid.php @@ -366,7 +366,7 @@ protected function prime_product_variations( $product_ids ) { if ( empty( $prime_variation_ids ) ) { return; } - + $all_variation_meta_data = $wpdb->get_results( $wpdb->prepare( "SELECT post_id as variation_id, meta_key as attribute_key, meta_value as attribute_value FROM {$wpdb->postmeta} WHERE post_id IN (" . implode( ',', array_map( 'esc_sql', $prime_variation_ids ) ) . ') AND meta_key LIKE %s', From 23824fb067b787afb129499e54f7a6457877df4e Mon Sep 17 00:00:00 2001 From: Raluca Stan Date: Thu, 30 Dec 2021 14:38:03 +0100 Subject: [PATCH 4/4] Remove unnecessary variable declaration --- src/BlockTypes/AbstractProductGrid.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/BlockTypes/AbstractProductGrid.php b/src/BlockTypes/AbstractProductGrid.php index 4b567756905..25bb57c4b02 100644 --- a/src/BlockTypes/AbstractProductGrid.php +++ b/src/BlockTypes/AbstractProductGrid.php @@ -361,7 +361,6 @@ protected function prime_product_variations( $product_ids ) { $product_variations = $wpdb->get_results( "SELECT ID as variation_id, post_parent as product_id from {$wpdb->posts} WHERE post_parent IN ( " . implode( ',', $prime_product_ids ) . ' )', ARRAY_A ); $prime_variation_ids = array_column( $product_variations, 'variation_id' ); $variation_ids_by_parent = array_column( $product_variations, 'product_id', 'variation_id' ); - $all_variation_meta_data = []; if ( empty( $prime_variation_ids ) ) { return;