Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WooCommerce product attributes lookup table for catalog filtering #4002

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bin/es-docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '2.2'
services:
elasticsearch:
build:
Expand Down
25 changes: 25 additions & 0 deletions includes/classes/Feature/WooCommerce/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion tests/php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
18 changes: 15 additions & 3 deletions tests/php/features/WooCommerce/TestWooCommerceProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down
Loading