diff --git a/plugins/woocommerce/changelog/fix-37601 b/plugins/woocommerce/changelog/fix-37601 new file mode 100644 index 00000000000..eeea8c4ae5f --- /dev/null +++ b/plugins/woocommerce/changelog/fix-37601 @@ -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. \ No newline at end of file diff --git a/plugins/woocommerce/includes/abstracts/abstract-wc-product.php b/plugins/woocommerce/includes/abstracts/abstract-wc-product.php index ac1eb1147ea..16d87eac306 100644 --- a/plugins/woocommerce/includes/abstracts/abstract-wc-product.php +++ b/plugins/woocommerce/includes/abstracts/abstract-wc-product.php @@ -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 ); } + /** + * 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. * diff --git a/plugins/woocommerce/includes/class-wc-product-variable.php b/plugins/woocommerce/includes/class-wc-product-variable.php index a240c406a08..ecab09b4cd3 100644 --- a/plugins/woocommerce/includes/class-wc-product-variable.php +++ b/plugins/woocommerce/includes/class-wc-product-variable.php @@ -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. * diff --git a/plugins/woocommerce/includes/wc-template-functions.php b/plugins/woocommerce/includes/wc-template-functions.php index e2162cef7b9..57384a00ca2 100644 --- a/plugins/woocommerce/includes/wc-template-functions.php +++ b/plugins/woocommerce/includes/wc-template-functions.php @@ -1365,12 +1365,17 @@ if ( ! function_exists( 'woocommerce_template_loop_add_to_cart' ) ) { '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', ), ); $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'] ) ) { $args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] ); }