From babde741db3dc4646a22dcd7a879d69112f85678 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 14 Nov 2017 11:45:16 +0000 Subject: [PATCH] Add add_to_cart_description method and aria-labels to cart buttons in the loop --- includes/abstracts/abstract-wc-product.php | 11 +++++++++++ includes/class-wc-product-external.php | 11 +++++++++++ includes/class-wc-product-grouped.php | 11 +++++++++++ includes/class-wc-product-simple.php | 13 +++++++++++++ includes/class-wc-product-variable.php | 11 +++++++++++ templates/loop/add-to-cart.php | 7 ++++--- 6 files changed, 61 insertions(+), 3 deletions(-) diff --git a/includes/abstracts/abstract-wc-product.php b/includes/abstracts/abstract-wc-product.php index ff70857907a..949fc7202e4 100644 --- a/includes/abstracts/abstract-wc-product.php +++ b/includes/abstracts/abstract-wc-product.php @@ -1760,6 +1760,17 @@ class WC_Product extends WC_Abstract_Legacy_Product { return apply_filters( 'woocommerce_product_add_to_cart_text', __( 'Read more', 'woocommerce' ), $this ); } + /** + * Get the add to cart button text description - used in aria tags. + * + * @since 3.3.0 + * @return string + */ + public function add_to_cart_description() { + /* translators: %s: Product title */ + return apply_filters( 'woocommerce_product_add_to_cart_description', sprintf( __( 'Read more about “%s”', 'woocommerce' ), $this->get_name() ), $this ); + } + /** * Returns the main product image. * diff --git a/includes/class-wc-product-external.php b/includes/class-wc-product-external.php index d9b7e72964a..114059d9907 100644 --- a/includes/class-wc-product-external.php +++ b/includes/class-wc-product-external.php @@ -180,4 +180,15 @@ class WC_Product_External extends WC_Product { public function add_to_cart_text() { return apply_filters( 'woocommerce_product_add_to_cart_text', $this->get_button_text() ? $this->get_button_text() : _x( 'Buy product', 'placeholder', 'woocommerce' ), $this ); } + + /** + * Get the add to cart button text description - used in aria tags. + * + * @since 3.3.0 + * @return string + */ + public function add_to_cart_description() { + /* translators: %s: Product title */ + return apply_filters( 'woocommerce_product_add_to_cart_description', $this->get_button_text() ? $this->get_button_text() : sprintf( __( 'Buy “%s”', 'woocommerce' ), $this->get_name() ), $this ); + } } diff --git a/includes/class-wc-product-grouped.php b/includes/class-wc-product-grouped.php index c10e4d28949..a9de2284881 100644 --- a/includes/class-wc-product-grouped.php +++ b/includes/class-wc-product-grouped.php @@ -43,6 +43,17 @@ class WC_Product_Grouped extends WC_Product { return apply_filters( 'woocommerce_product_add_to_cart_text', __( 'View products', 'woocommerce' ), $this ); } + /** + * Get the add to cart button text description - used in aria tags. + * + * @since 3.3.0 + * @return string + */ + public function add_to_cart_description() { + /* translators: %s: Product title */ + return apply_filters( 'woocommerce_product_add_to_cart_description', sprintf( __( 'View products in the “%s” group', 'woocommerce' ), $this->get_name() ), $this ); + } + /** * Returns whether or not the product is on sale. * diff --git a/includes/class-wc-product-simple.php b/includes/class-wc-product-simple.php index 33ee968fde4..742166d46de 100644 --- a/includes/class-wc-product-simple.php +++ b/includes/class-wc-product-simple.php @@ -55,4 +55,17 @@ class WC_Product_Simple extends WC_Product { return apply_filters( 'woocommerce_product_add_to_cart_text', $text, $this ); } + + /** + * Get the add to cart button text description - used in aria tags. + * + * @since 3.3.0 + * @return string + */ + public function add_to_cart_description() { + /* translators: %s: Product title */ + $text = $this->is_purchasable() && $this->is_in_stock() ? __( 'Add “%s” to your cart', 'woocommerce' ) : __( 'Read more about “%s”', 'woocommerce' ); + + return apply_filters( 'woocommerce_product_add_to_cart_description', sprintf( $text, $this->get_name() ), $this ); + } } diff --git a/includes/class-wc-product-variable.php b/includes/class-wc-product-variable.php index f372f4318e8..42512b71e89 100644 --- a/includes/class-wc-product-variable.php +++ b/includes/class-wc-product-variable.php @@ -60,6 +60,17 @@ class WC_Product_Variable extends WC_Product { return apply_filters( 'woocommerce_product_add_to_cart_text', $this->is_purchasable() ? __( 'Select options', 'woocommerce' ) : __( 'Read more', 'woocommerce' ), $this ); } + /** + * Get the add to cart button text description - used in aria tags. + * + * @since 3.3.0 + * @return string + */ + public function add_to_cart_description() { + /* translators: %s: Product title */ + return apply_filters( 'woocommerce_product_add_to_cart_description', sprintf( __( 'Select options for “%s”', 'woocommerce' ), $this->get_name() ), $this ); + } + /** * Get an array of all sale and regular prices from all variations. This is used for example when displaying the price range at variable product level or seeing if the variable product is on sale. * diff --git a/templates/loop/add-to-cart.php b/templates/loop/add-to-cart.php index 5a20a0cdb31..15d12082829 100644 --- a/templates/loop/add-to-cart.php +++ b/templates/loop/add-to-cart.php @@ -13,7 +13,7 @@ * @see https://docs.woocommerce.com/document/template-structure/ * @author WooThemes * @package WooCommerce/Templates - * @version 3.0.0 + * @version 3.3.0 */ if ( ! defined( 'ABSPATH' ) ) { @@ -22,13 +22,14 @@ if ( ! defined( 'ABSPATH' ) ) { global $product; -echo apply_filters( 'woocommerce_loop_add_to_cart_link', - sprintf( '%s', +echo apply_filters( 'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok. + sprintf( '%s', esc_url( $product->add_to_cart_url() ), esc_attr( isset( $quantity ) ? $quantity : 1 ), esc_attr( $product->get_id() ), esc_attr( $product->get_sku() ), esc_attr( isset( $class ) ? $class : 'button' ), + esc_attr( $product->add_to_cart_description() ), esc_html( $product->add_to_cart_text() ) ), $product );