Fix/37601 Add `aria-describedby` to Select options button (#37880)

* Add `aria-describedby` to select options button

* add a changelog

* update the chnagelog file

* fix type

* fix the function name

* remove `aria-describedby`s if with value

* suggestions implemented

* Minor PHPCS fixes

---------

Co-authored-by: Jorge A. Torres <jorge.torres@automattic.com>
This commit is contained in:
Faisal Alvi 2023-05-13 00:46:38 +05:30 committed by GitHub
parent 0ea5205672
commit 8e9ff0d7f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,5 @@
Significance: minor
Type: add
Add a function to get the aria-describedby description for the add to cart button.
Add default description for the Select options button.

View File

@ -1932,6 +1932,23 @@ class WC_Product extends WC_Abstract_Legacy_Product {
return apply_filters( 'woocommerce_product_single_add_to_cart_text', __( 'Add to cart', 'woocommerce' ), $this ); return apply_filters( 'woocommerce_product_single_add_to_cart_text', __( 'Add to cart', 'woocommerce' ), $this );
} }
/**
* Get the aria-describedby description for the add to cart button.
*
* @return string
*/
public function add_to_cart_aria_describedby() {
/**
* Filter the aria-describedby description for the add to cart button.
*
* @since 7.8.0
*
* @param string $var Text for the 'aria-describedby' attribute.
* @param WC_Product $this Product object.
*/
return apply_filters( 'woocommerce_product_add_to_cart_aria_describedby', '', $this );
}
/** /**
* Get the add to cart button text. * Get the add to cart button text.
* *

View File

@ -51,6 +51,20 @@ class WC_Product_Variable extends WC_Product {
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
/**
* Get the aria-describedby description for the add to cart button.
*
* @return string
*/
public function add_to_cart_aria_describedby() {
/**
* This filter is documented in includes/abstracts/abstract-wc-product.php.
*
* @since 7.8.0
*/
return apply_filters( 'woocommerce_product_add_to_cart_aria_describedby', $this->is_purchasable() ? __( 'This product has multiple variants. The options may be chosen on the product page', 'woocommerce' ) : '', $this );
}
/** /**
* Get the add to cart button text. * Get the add to cart button text.
* *

View File

@ -1365,12 +1365,17 @@ if ( ! function_exists( 'woocommerce_template_loop_add_to_cart' ) ) {
'data-product_id' => $product->get_id(), 'data-product_id' => $product->get_id(),
'data-product_sku' => $product->get_sku(), 'data-product_sku' => $product->get_sku(),
'aria-label' => $product->add_to_cart_description(), 'aria-label' => $product->add_to_cart_description(),
'aria-describedby' => $product->add_to_cart_aria_describedby(),
'rel' => 'nofollow', 'rel' => 'nofollow',
), ),
); );
$args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product ); $args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );
if ( ! empty( $args['attributes']['aria-describedby'] ) ) {
$args['attributes']['aria-describedby'] = wp_strip_all_tags( $args['attributes']['aria-describedby'] );
}
if ( isset( $args['attributes']['aria-label'] ) ) { if ( isset( $args['attributes']['aria-label'] ) ) {
$args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] ); $args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] );
} }