diff --git a/plugins/woocommerce/changelog/31099 b/plugins/woocommerce/changelog/31099 new file mode 100644 index 00000000000..c7d42720bfe --- /dev/null +++ b/plugins/woocommerce/changelog/31099 @@ -0,0 +1,4 @@ +Significance: minor +Type: enhancement + +Improve accessibility of sale price markup. diff --git a/plugins/woocommerce/includes/wc-formatting-functions.php b/plugins/woocommerce/includes/wc-formatting-functions.php index 52084b31291..2549d909bfb 100644 --- a/plugins/woocommerce/includes/wc-formatting-functions.php +++ b/plugins/woocommerce/includes/wc-formatting-functions.php @@ -1290,7 +1290,28 @@ function wc_format_stock_quantity_for_display( $stock_quantity, $product ) { * @return string */ function wc_format_sale_price( $regular_price, $sale_price ) { - $price = ' ' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . ''; + // Format the prices. + $formatted_regular_price = is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price; + $formatted_sale_price = is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price; + + // Strikethrough pricing. + $price = ' '; + + // For accessibility (a11y) we'll also display that information to screen readers. + $price .= ''; + // translators: %s is a product's regular price. + $price .= esc_html( sprintf( __( 'Original price was: %s.', 'woocommerce' ), wp_strip_all_tags( $formatted_regular_price ) ) ); + $price .= ''; + + // Add the sale price. + $price .= ''; + + // For accessibility (a11y) we'll also display that information to screen readers. + $price .= ''; + // translators: %s is a product's current (sale) price. + $price .= esc_html( sprintf( __( 'Current price is: %s.', 'woocommerce' ), wp_strip_all_tags( $formatted_sale_price ) ) ); + $price .= ''; + return apply_filters( 'woocommerce_format_sale_price', $price, $regular_price, $sale_price ); } diff --git a/plugins/woocommerce/tests/e2e-pw/tests/merchant/product-edit.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/merchant/product-edit.spec.js index 9564a72222d..a9c8501b013 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/merchant/product-edit.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/merchant/product-edit.spec.js @@ -163,7 +163,7 @@ baseTest.describe( 'Products > Edit Product', () => { .soft( page.locator( 'p' ).locator( 'del' ) ) .toHaveText( `$${ expectedRegularPrice }` ); await expect - .soft( page.locator( 'p' ).getByRole( 'insertion' ) ) + .soft( page.locator( 'p' ).locator( 'ins' ) ) .toHaveText( `$${ expectedSalePrice }` ); await expect .soft( page.getByText( `${ expectedStockQty } in stock` ) ) diff --git a/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php b/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php index 5759750c101..a2f57041ffe 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php @@ -932,7 +932,7 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case { * @since 3.3.0 */ public function test_wc_format_sale_price() { - $this->assertEquals( ' $5.00', wc_format_sale_price( '10', '5' ) ); + $this->assertEquals( ' Original price was: $10.00.Current price is: $5.00.', wc_format_sale_price( '10', '5' ) ); } /** diff --git a/plugins/woocommerce/tests/legacy/unit-tests/product/data.php b/plugins/woocommerce/tests/legacy/unit-tests/product/data.php index ae732e1a3f5..dab7f83d71f 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/product/data.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/product/data.php @@ -262,11 +262,11 @@ class WC_Tests_Product_Data extends WC_Unit_Test_Case { $product = wc_get_product( $product1_id ); $this->assertEquals( $product1_id, $product->get_id() ); - $this->assertEquals( ' $7.00', $product->get_price_html() ); + $this->assertEquals( ' Original price was: $10.00.Current price is: $7.00.', $product->get_price_html() ); $product = wc_get_product( $product2_id ); $this->assertEquals( $product2_id, $product->get_id() ); - $this->assertEquals( ' $16.00', $product->get_price_html() ); + $this->assertEquals( ' Original price was: $20.00.Current price is: $16.00.', $product->get_price_html() ); $product = wc_get_product( $product3_id ); $this->assertEquals( $product3_id, $product->get_id() );