diff --git a/plugins/woocommerce/changelog/50567-update-match-redirected-product-page b/plugins/woocommerce/changelog/50567-update-match-redirected-product-page new file mode 100644 index 00000000000..64e284dd36f --- /dev/null +++ b/plugins/woocommerce/changelog/50567-update-match-redirected-product-page @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Treat post_type=product as a shop page. \ No newline at end of file diff --git a/plugins/woocommerce/src/Admin/WCAdminHelper.php b/plugins/woocommerce/src/Admin/WCAdminHelper.php index 19518a29b5d..9e234762eb8 100644 --- a/plugins/woocommerce/src/Admin/WCAdminHelper.php +++ b/plugins/woocommerce/src/Admin/WCAdminHelper.php @@ -150,6 +150,18 @@ class WCAdminHelper { } $normalized_path = self::get_normalized_url_path( $url ); + $params = array( + 'post_type' => 'product', + ); + + parse_str( wp_parse_url( $url, PHP_URL_QUERY ), $url_params ); + + foreach ( $params as $key => $param ) { + if ( isset( $url_params[ $key ] ) && $url_params[ $key ] === $param ) { + return true; + } + } + // WC store pages. $store_pages = array( 'shop' => wc_get_page_id( 'shop' ), @@ -185,6 +197,7 @@ class WCAdminHelper { } $permalink = get_permalink( $page_id ); + if ( ! $permalink ) { continue; } diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/wc-admin-helper.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/wc-admin-helper.php index 46608767664..c7a712d197c 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/wc-admin-helper.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/wc-admin-helper.php @@ -257,6 +257,19 @@ class WC_Admin_Tests_Admin_Helper extends WC_Unit_Test_Case { } } + /** + * Test is_store_page with the defined post_type param. + */ + public function test_is_store_page_with_post_type() { + // Test with post_type=product. + $this->assertTrue( WCAdminHelper::is_store_page( 'https://example.com/?post_type=product' ) ); + // Test with post_type=product and other params. + $this->assertTrue( WCAdminHelper::is_store_page( 'https://example.com/test?param1=value1&post_type=product¶m2=value2' ) ); + + // should return false if post_type is not product. + $this->assertFalse( WCAdminHelper::is_store_page( 'https://example.com/test?param1=value1¶m2=value2' ) ); + } + /** Test product archive link is store page even if shop page not set. */ public function test_is_store_page_even_if_shop_page_not_set() { $shop_page_id = get_option( 'woocommerce_shop_page_id' );