Commit 3bce0a8cefa for woocommerce

commit 3bce0a8cefa4486bdbe95f47fb7f03a390ceb06f
Author: Ethan Clevenger <ethan.c.clevenger@gmail.com>
Date:   Mon Nov 16 21:16:03 2026 -0800

    Allow classic themes to opt into a11y labels for orderby dropdown (#66476)

    * Refactor references to array value to a filtered var.

    * Add inline docs.

    * Add changefile(s) from automation for the following project(s): woocommerce

    * Fix the orderby label filter docblock and add a test

    Set the real @since version, remove trailing spaces that fail lint, and
    cast the filter result to a bool so a bad callback value cannot leak
    into the template. Add a test that the filter turns on the label.

    Amp-Thread-ID: https://ampcode.com/threads/T-bd8dff58-f97a-42de-978c-ff342adeafd4
    Co-authored-by: Amp <amp@ampcode.com>

    * Reword the changelog entry for the orderby label filter

    Amp-Thread-ID: https://ampcode.com/threads/T-bd8dff58-f97a-42de-978c-ff342adeafd4
    Co-authored-by: Amp <amp@ampcode.com>

    * Add changefile(s) from automation for the following project(s): woocommerce

    * Check that the orderby label points to the select in the test

    The old pattern also passed when the matching id was on another element.

    * Limit the orderby label filter to classic themes and rename it

    The Catalog Sorting block always passes its useLabel setting, so the
    filter now runs only when no block setting is given. Rename the filter
    to woocommerce_catalog_orderby_show_visual_label, as asked in review.

    Amp-Thread-ID: https://ampcode.com/threads/T-bd8dff58-f97a-42de-978c-ff342adeafd4
    Co-authored-by: Amp <amp@ampcode.com>

    * Add changefile(s) from automation for the following project(s): woocommerce

    * Add changefile(s) from automation for the following project(s): woocommerce

    * Simplify the orderby label filter docblock and test name

    Use plain wording in the docblock, and name the test after the new filter.

    Amp-Thread-ID: https://ampcode.com/threads/T-bd8dff58-f97a-42de-978c-ff342adeafd4
    Co-authored-by: Amp <amp@ampcode.com>

    ---------

    Co-authored-by: woocommercebot <woocommercebot@users.noreply.github.com>
    Co-authored-by: Tung Du <tung.du@automattic.com>
    Co-authored-by: Amp <amp@ampcode.com>

diff --git a/plugins/woocommerce/changelog/66476-feature-orderby-label-legacy-optin b/plugins/woocommerce/changelog/66476-feature-orderby-label-legacy-optin
new file mode 100644
index 00000000000..86064663f09
--- /dev/null
+++ b/plugins/woocommerce/changelog/66476-feature-orderby-label-legacy-optin
@@ -0,0 +1,4 @@
+Significance: minor
+Type: enhancement
+
+Add the `woocommerce_catalog_orderby_show_visual_label` filter. Classic themes can use it to show a visible "Sort by" label on the product sorting dropdown.
diff --git a/plugins/woocommerce/includes/wc-template-functions.php b/plugins/woocommerce/includes/wc-template-functions.php
index 37dd2c98868..a45de76e5b1 100644
--- a/plugins/woocommerce/includes/wc-template-functions.php
+++ b/plugins/woocommerce/includes/wc-template-functions.php
@@ -1683,6 +1683,22 @@ if ( ! function_exists( 'woocommerce_catalog_ordering' ) ) {
 			return;
 		}

+		if ( isset( $attributes['useLabel'] ) ) {
+			$use_label = (bool) $attributes['useLabel'];
+		} else {
+			/**
+			 * Filters whether the product sorting dropdown shows a visible "Sort by" label.
+			 *
+			 * Return true to show the label in classic themes. The Catalog Sorting block
+			 * does not use this filter. It has its own "Show label" setting.
+			 *
+			 * @since 11.4.0
+			 *
+			 * @param bool $show_label Whether to show the label. Default false.
+			 */
+			$use_label = (bool) apply_filters( 'woocommerce_catalog_orderby_show_visual_label', false );
+		}
+
 		/**
 		 * Filter the default catalog orderby.
 		 *
@@ -1692,7 +1708,7 @@ if ( ! function_exists( 'woocommerce_catalog_ordering' ) ) {
 		 */
 		$show_default_orderby = 'menu_order' === apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby', 'menu_order' ) );

-		if ( isset( $attributes ) && isset( $attributes['useLabel'] ) && $attributes['useLabel'] ) {
+		if ( $use_label ) {
 			/**
 			 * Filters the catalog orderby options.
 			 *
@@ -1758,7 +1774,7 @@ if ( ! function_exists( 'woocommerce_catalog_ordering' ) ) {
 				'catalog_orderby_options' => $catalog_orderby_options,
 				'orderby'                 => $orderby,
 				'show_default_orderby'    => $show_default_orderby,
-				'use_label'               => isset( $attributes['useLabel'] ) ? $attributes['useLabel'] : false,
+				'use_label'               => $use_label,
 			)
 		);
 	}
diff --git a/plugins/woocommerce/tests/legacy/unit-tests/templates/functions.php b/plugins/woocommerce/tests/legacy/unit-tests/templates/functions.php
index 079abf55d3b..5b27c7ed067 100644
--- a/plugins/woocommerce/tests/legacy/unit-tests/templates/functions.php
+++ b/plugins/woocommerce/tests/legacy/unit-tests/templates/functions.php
@@ -491,6 +491,45 @@ class WC_Tests_Template_Functions extends WC_Unit_Test_Case {
 		$this->assertEquals( $expected_html, $actual_html );
 	}

+	/**
+	 * Test that the woocommerce_catalog_orderby_show_visual_label filter shows the "Sort by" label in classic themes.
+	 * The Catalog Sorting block passes its own setting and ignores the filter.
+	 */
+	public function test_woocommerce_catalog_ordering_show_visual_label_filter() {
+		wc_setup_loop(
+			array(
+				'is_paginated' => true,
+				'total'        => 1,
+			)
+		);
+
+		ob_start();
+		woocommerce_catalog_ordering();
+		$without_filter = ob_get_clean();
+
+		add_filter( 'woocommerce_catalog_orderby_show_visual_label', '__return_true' );
+		ob_start();
+		woocommerce_catalog_ordering();
+		$with_filter = ob_get_clean();
+		ob_start();
+		woocommerce_catalog_ordering( array( 'useLabel' => false ) );
+		$block_with_filter = ob_get_clean();
+		remove_filter( 'woocommerce_catalog_orderby_show_visual_label', '__return_true' );
+
+		wc_reset_loop();
+
+		$this->assertStringNotContainsString( '<label', $without_filter );
+		$this->assertStringContainsString( 'aria-label="Shop order"', $without_filter );
+		$this->assertStringContainsString( '>Default sorting</option>', $without_filter );
+
+		$this->assertMatchesRegularExpression( '/<label for="(woocommerce-orderby-[^"]+)">Sort by<\/label>\s*<select[^>]*\sid="\1"/', $with_filter );
+		$this->assertStringNotContainsString( 'aria-label="Shop order"', $with_filter );
+		$this->assertStringContainsString( '>Default</option>', $with_filter );
+
+		$this->assertStringNotContainsString( '<label', $block_with_filter );
+		$this->assertStringContainsString( 'aria-label="Shop order"', $block_with_filter );
+	}
+
 	/**
 	 * Test wc_add_aria_label_to_pagination_numbers with basic pagination links
 	 */