Skip to content

Commit

Permalink
Add HPOS compatibility notice for WooCommerce Orders
Browse files Browse the repository at this point in the history
felipeelia committed Mar 15, 2024
1 parent 66ebdd9 commit af927dd
Showing 2 changed files with 84 additions and 0 deletions.
43 changes: 43 additions & 0 deletions includes/classes/Feature/WooCommerce/Orders.php
Original file line number Diff line number Diff line change
@@ -45,6 +45,7 @@ public function setup() {
add_action( 'parse_query', [ $this, 'maybe_hook_woocommerce_search_fields' ], 1 );
add_action( 'parse_query', [ $this, 'search_order' ], 11 );
add_action( 'pre_get_posts', [ $this, 'translate_args' ], 11, 1 );
add_filter( 'ep_admin_notices', [ $this, 'hpos_compatibility_notice' ] );
}

/**
@@ -335,6 +336,48 @@ public function get_supported_post_types() : array {
return $supported_post_types;
}

/**
* Display a notice if WooCommerce Orders are not compatible with ElasticPress
*
* If the user has WooCommerce, Protected Content, and HPOS enabled, orders will not go through ElasticPress.
*
* @param array $notices Current EP notices
* @return array
*/
public function hpos_compatibility_notice( array $notices ) : array {
$current_screen = \get_current_screen();
if ( empty( $current_screen->id ) || 'woocommerce_page_wc-orders' !== $current_screen->id ) {
return $notices;
}

if ( \ElasticPress\Utils\get_option( 'ep_hide_wc_orders_incompatible_notice' ) ) {
return $notices;
}

$protected_content = \ElasticPress\Features::factory()->get_registered_feature( 'protected_content' );
if ( ! $protected_content->is_active() ) {
return $notices;
}

if (
! class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' )
|| ! method_exists( '\Automattic\WooCommerce\Utilities\OrderUtil', 'custom_orders_table_usage_is_enabled' ) ) {
return $notices;
}

if ( ! \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled() ) {
return $notices;
}

$notices['wc_orders_incompatible'] = [
'html' => esc_html__( "Although the WooCommerce and Protected Content features are enabled, ElasticPress will not integrate with the WooCommerce Orders list if WooCommerce's High-performance order storage is enabled.", 'elasticpress' ),
'type' => 'warning',
'dismiss' => true,
];

return $notices;
}

/**
* If the query has a search term, add the order fields that need to be searched.
*
41 changes: 41 additions & 0 deletions tests/php/features/WooCommerce/TestWooCommerceOrders.php
Original file line number Diff line number Diff line change
@@ -329,4 +329,45 @@ public function ordersAutosuggestMethodsDataProvider() : array {
[ 'set_search_fields', [] ],
];
}

/**
* Test the `hpos_compatibility_notice` method
*
* @group woocommerce
* @group woocommerce-orders
*/
public function test_hpos_compatibility_notice() {
$notices = [
'test' => [],
];
$this->assertCount( 1, $this->orders->hpos_compatibility_notice( $notices ) );

\set_current_screen( 'woocommerce_page_wc-orders' );
$this->assertCount( 1, $this->orders->hpos_compatibility_notice( $notices ) );

ElasticPress\Features::factory()->activate_feature( 'protected_content' );
$this->assertCount( 1, $this->orders->hpos_compatibility_notice( $notices ) );

$option_name = \Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController::CUSTOM_ORDERS_TABLE_USAGE_ENABLED_OPTION;

Check warning on line 351 in tests/php/features/WooCommerce/TestWooCommerceOrders.php

GitHub Actions / PHP Lint

Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
$change_value = function() {
return 'yes';
};
add_filter( 'pre_option_' . $option_name, $change_value );

$new_notices = $this->orders->hpos_compatibility_notice( $notices );
$this->assertCount( 2, $new_notices );
$this->assertArrayHasKey( 'wc_orders_incompatible', $new_notices );

/**
* Test if the notice is hidden when the user already dismissed it
*/
$change_hide_option = function() {
return 1;
};
add_filter( 'pre_option_ep_hide_wc_orders_incompatible_notice', $change_hide_option );
add_filter( 'pre_site_option_ep_hide_wc_orders_incompatible_notice', $change_hide_option );
$new_notices = $this->orders->hpos_compatibility_notice( $notices );
$this->assertCount( 1, $new_notices );
$this->assertArrayNotHasKey( 'wc_orders_incompatible', $new_notices );
}
}

0 comments on commit af927dd

Please sign in to comment.