diff --git a/bin/es-docker/docker-compose.yml b/bin/es-docker/docker-compose.yml index 40ea02f43..5faa89e95 100644 --- a/bin/es-docker/docker-compose.yml +++ b/bin/es-docker/docker-compose.yml @@ -1,4 +1,3 @@ -version: '2.2' services: elasticsearch: build: diff --git a/includes/classes/Feature/WooCommerce/Products.php b/includes/classes/Feature/WooCommerce/Products.php index 312f80f13..134bddb2b 100644 --- a/includes/classes/Feature/WooCommerce/Products.php +++ b/includes/classes/Feature/WooCommerce/Products.php @@ -816,6 +816,31 @@ protected function maybe_update_tax_query( \WP_Query $query ) { } } + /* + * If the site is set to use the product attributes lookup table for catalog filtering + * we need to add filters back to WP_Query, so EP can handle the filtering. + */ + $filterer_class = 'Automattic\WooCommerce\Internal\ProductAttributesLookup\Filterer'; + if ( + function_exists( 'wc_get_container' ) && + class_exists( $filterer_class ) && + method_exists( 'WC_Query', 'get_layered_nav_chosen_attributes' ) + ) { + $filterer = wc_get_container()->get( $filterer_class ); + + if ( $filterer->filtering_via_lookup_table_is_active() ) { + foreach ( \WC_Query::get_layered_nav_chosen_attributes() as $taxonomy => $data ) { + $tax_query[] = array( + 'taxonomy' => $taxonomy, + 'field' => 'slug', + 'terms' => $data['terms'], + 'operator' => 'and' === $data['query_type'] ? 'AND' : 'IN', + 'include_children' => false, + ); + } + } + } + $query->set( 'tax_query', $tax_query ); } diff --git a/package.json b/package.json index 5086d8332..c88060508 100644 --- a/package.json +++ b/package.json @@ -35,9 +35,11 @@ "lint-style": "10up-toolkit lint-style", "env": "wp-env", "env:install-tests-cli": "./bin/install-wp-cli.sh tests-wordpress", - "env:start": "wp-env start && npm run env:install-tests-cli && cd bin/es-docker/ && docker compose build --build-arg ES_VERSION=${ES_VERSION-7.10.2} && docker compose up -d", - "env:stop": "wp-env stop && cd bin/es-docker/ && docker compose down", + "env:start": "wp-env start && npm run env:install-tests-cli && npm run es:start", + "env:stop": "wp-env stop && npm run es:stop", "env:reset": "wp-env clean all && npm run env:start && npm run cypress:setup", + "es:start": "cd bin/es-docker/ && docker compose build --build-arg ES_VERSION=${ES_VERSION-7.10.2} && docker compose up -d", + "es:stop": "cd bin/es-docker/ && docker compose down", "cypress:setup": "./bin/setup-cypress-env.sh", "cypress:open": "cypress open --config-file tests/cypress/config.js", "cypress:run": "cypress run --config-file tests/cypress/config.js" diff --git a/tests/php/bootstrap.php b/tests/php/bootstrap.php index bb6789b07..b8e2f46d9 100644 --- a/tests/php/bootstrap.php +++ b/tests/php/bootstrap.php @@ -38,7 +38,7 @@ function load_plugin() { $host = getenv( 'EP_HOST' ); if ( empty( $host ) ) { - $host = 'http://127.0.0.1:9200'; + $host = 'http://127.0.0.1:8890'; } update_option( 'ep_host', $host ); diff --git a/tests/php/features/WooCommerce/TestWooCommerceProduct.php b/tests/php/features/WooCommerce/TestWooCommerceProduct.php index 9eb590f89..c3c646b5e 100644 --- a/tests/php/features/WooCommerce/TestWooCommerceProduct.php +++ b/tests/php/features/WooCommerce/TestWooCommerceProduct.php @@ -694,11 +694,23 @@ function( \WP_Query $query ) { // mock the query as main query $wp_the_query = $query; - $query = $query->query( $args ); + $query_results = $query->query( $args ); + + $this->assertTrue( $wp_the_query->elasticsearch_success ); + $this->assertEquals( 1, count( $query_results ) ); + $this->assertEquals( 'Cap', $query_results[0]->post_title ); + + // Enable the product attributes lookup table for catalog filtering + $enable_attribute_lookup = function() { + return 'yes'; + }; + add_filter( 'pre_option_woocommerce_attribute_lookup_enabled', $enable_attribute_lookup ); + + $query_results = $query->query( $args ); $this->assertTrue( $wp_the_query->elasticsearch_success ); - $this->assertEquals( 1, count( $query ) ); - $this->assertEquals( 'Cap', $query[0]->post_title ); + $this->assertEquals( 1, count( $query_results ) ); + $this->assertEquals( 'Cap', $query_results[0]->post_title ); } /**