Fix incorrectly used aria-describedby in add to cart button of variable product (#46897)

This commit is contained in:
Sam Seay 2024-04-29 17:31:34 +08:00 committed by GitHub
parent a9c4dedc20
commit 05f14d9465
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 20 additions and 9 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: fix
Fix an accessibility error in the add to cart button template.

View File

@ -53,6 +53,8 @@ class WC_Product_Variable extends WC_Product {
/**
* Get the aria-describedby description for the add to cart button.
* Note that this is to provide the description, not the describedby attribute
* itself.
*
* @return string
*/

View File

@ -470,7 +470,7 @@ function wc_get_loop_class() {
$loop_index = wc_get_loop_prop( 'loop', 0 );
$columns = absint( max( 1, wc_get_loop_prop( 'columns', wc_get_default_products_per_row() ) ) );
$loop_index ++;
++$loop_index;
wc_set_loop_prop( 'loop', $loop_index );
if ( 0 === ( $loop_index - 1 ) % $columns || 1 === $columns ) {
@ -1365,8 +1365,8 @@ if ( ! function_exists( 'woocommerce_template_loop_add_to_cart' ) ) {
if ( $product ) {
$defaults = array(
'quantity' => 1,
'class' => implode(
'quantity' => 1,
'class' => implode(
' ',
array_filter(
array(
@ -1378,11 +1378,11 @@ if ( ! function_exists( 'woocommerce_template_loop_add_to_cart' ) ) {
)
)
),
'attributes' => array(
'aria-describedby_text' => $product->add_to_cart_aria_describedby(),
'attributes' => array(
'data-product_id' => $product->get_id(),
'data-product_sku' => $product->get_sku(),
'aria-label' => $product->add_to_cart_description(),
'aria-describedby' => $product->add_to_cart_aria_describedby(),
'rel' => 'nofollow',
),
);
@ -3519,7 +3519,7 @@ if ( ! function_exists( 'wc_display_item_downloads' ) ) {
if ( ! empty( $downloads ) ) {
$i = 0;
foreach ( $downloads as $file ) {
$i ++;
++$i;
if ( $args['show_url'] ) {
$strings[] = '<strong class="wc-item-download-label">' . esc_html( $file['name'] ) . ':</strong> ' . esc_html( $file['download_url'] );
@ -3989,7 +3989,7 @@ function wc_get_pay_buttons() {
echo '<div class="woocommerce-pay-buttons">';
foreach ( $supported_gateways as $pay_button_id ) {
echo sprintf( '<div class="woocommerce-pay-button__%1$s %1$s" id="%1$s"></div>', esc_attr( $pay_button_id ) );
printf( '<div class="woocommerce-pay-button__%1$s %1$s" id="%1$s"></div>', esc_attr( $pay_button_id ) );
}
echo '</div>';
}

View File

@ -12,7 +12,7 @@
*
* @see https://woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 3.3.0
* @version 9.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
@ -24,8 +24,9 @@ global $product;
echo apply_filters(
'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
sprintf(
'<a href="%s" data-quantity="%s" class="%s" %s>%s</a>',
'<a href="%s" aria-describedby="woocommerce_loop_add_to_cart_link_describedby_%s" data-quantity="%s" class="%s" %s>%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( $product->get_id() ),
esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
@ -34,3 +35,7 @@ echo apply_filters(
$product,
$args
);
?>
<span id="woocommerce_loop_add_to_cart_link_describedby_<?php echo esc_attr( $product->get_id() ); ?>" class="screen-reader-text">
<?php echo esc_html( $args['aria-describedby_text'] ); ?>
</span>