diff --git a/plugins/woocommerce/changelog/51316-fix-51216-filter-by-stock-block-fails-with-ansi_quotes-enabled-database b/plugins/woocommerce/changelog/51316-fix-51216-filter-by-stock-block-fails-with-ansi_quotes-enabled-database new file mode 100644 index 00000000000..37eb924dbb5 --- /dev/null +++ b/plugins/woocommerce/changelog/51316-fix-51216-filter-by-stock-block-fails-with-ansi_quotes-enabled-database @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Fix: Multiple issues related to MySQL having `ANSI_QUOTES` enabled in Product Filters \ No newline at end of file diff --git a/plugins/woocommerce/src/Blocks/QueryFilters.php b/plugins/woocommerce/src/Blocks/QueryFilters.php index 274566be8b0..d3e783ba752 100644 --- a/plugins/woocommerce/src/Blocks/QueryFilters.php +++ b/plugins/woocommerce/src/Blocks/QueryFilters.php @@ -209,7 +209,7 @@ final class QueryFilters { } $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); - $args['where'] .= ' AND wc_product_meta_lookup.stock_status IN ("' . implode( '","', array_map( 'esc_sql', explode( ',', $wp_query->get( 'filter_stock_status' ) ) ) ) . '")'; + $args['where'] .= ' AND wc_product_meta_lookup.stock_status IN (\'' . implode( '\',\'', array_map( 'esc_sql', explode( ',', $wp_query->get( 'filter_stock_status' ) ) ) ) . '\')'; return $args; } diff --git a/plugins/woocommerce/src/StoreApi/Utilities/ProductQuery.php b/plugins/woocommerce/src/StoreApi/Utilities/ProductQuery.php index 3fce3214ab0..ddcbc15fea6 100644 --- a/plugins/woocommerce/src/StoreApi/Utilities/ProductQuery.php +++ b/plugins/woocommerce/src/StoreApi/Utilities/ProductQuery.php @@ -359,7 +359,7 @@ class ProductQuery { $skus[] = $wp_query->get( 'sku' ); } $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); - $args['where'] .= ' AND wc_product_meta_lookup.sku IN ("' . implode( '","', array_map( 'esc_sql', $skus ) ) . '")'; + $args['where'] .= ' AND wc_product_meta_lookup.sku IN (\'' . implode( '\',\'', array_map( 'esc_sql', $skus ) ) . '\')'; } if ( $wp_query->get( 'slug' ) ) { @@ -375,10 +375,10 @@ class ProductQuery { if ( $wp_query->get( 'stock_status' ) ) { $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); - $args['where'] .= ' AND wc_product_meta_lookup.stock_status IN ("' . implode( '","', array_map( 'esc_sql', $wp_query->get( 'stock_status' ) ) ) . '")'; + $args['where'] .= ' AND wc_product_meta_lookup.stock_status IN (\'' . implode( '\',\'', array_map( 'esc_sql', $wp_query->get( 'stock_status' ) ) ) . '\')'; } elseif ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); - $args['where'] .= ' AND wc_product_meta_lookup.stock_status NOT IN ("outofstock")'; + $args['where'] .= ' AND wc_product_meta_lookup.stock_status NOT IN (\'outofstock\')'; } if ( $wp_query->get( 'min_price' ) || $wp_query->get( 'max_price' ) ) { diff --git a/plugins/woocommerce/src/StoreApi/Utilities/ProductQueryFilters.php b/plugins/woocommerce/src/StoreApi/Utilities/ProductQueryFilters.php index 6d136d65e8d..9afe4db151b 100644 --- a/plugins/woocommerce/src/StoreApi/Utilities/ProductQueryFilters.php +++ b/plugins/woocommerce/src/StoreApi/Utilities/ProductQueryFilters.php @@ -153,8 +153,9 @@ class ProductQueryFilters { }, $attributes ); - $attributes_to_count_sql = 'AND term_taxonomy.taxonomy IN ("' . implode( '","', $attributes_to_count ) . '")'; - $attribute_count_sql = " + $attributes_to_count_sql = 'AND term_taxonomy.taxonomy IN (\'' . implode( '\',\'', $attributes_to_count ) . '\')'; + + $attribute_count_sql = " SELECT COUNT( DISTINCT posts.ID ) as term_count, terms.term_id as term_count_id FROM {$wpdb->posts} AS posts INNER JOIN {$wpdb->term_relationships} AS term_relationships ON posts.ID = term_relationships.object_id