Add add_to_cart_description method and aria-labels to cart buttons in the loop

This commit is contained in:
Mike Jolley 2017-11-14 11:45:16 +00:00
parent 387093cd27
commit babde741db
6 changed files with 61 additions and 3 deletions

View File

@ -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 ); 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. * Returns the main product image.
* *

View File

@ -180,4 +180,15 @@ class WC_Product_External extends WC_Product {
public function add_to_cart_text() { 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 ); 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 );
}
} }

View File

@ -43,6 +43,17 @@ class WC_Product_Grouped extends WC_Product {
return apply_filters( 'woocommerce_product_add_to_cart_text', __( 'View products', 'woocommerce' ), $this ); 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. * Returns whether or not the product is on sale.
* *

View File

@ -55,4 +55,17 @@ class WC_Product_Simple extends WC_Product {
return apply_filters( 'woocommerce_product_add_to_cart_text', $text, $this ); 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 );
}
} }

View File

@ -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 ); 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. * 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.
* *

View File

@ -13,7 +13,7 @@
* @see https://docs.woocommerce.com/document/template-structure/ * @see https://docs.woocommerce.com/document/template-structure/
* @author WooThemes * @author WooThemes
* @package WooCommerce/Templates * @package WooCommerce/Templates
* @version 3.0.0 * @version 3.3.0
*/ */
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
@ -22,13 +22,14 @@ if ( ! defined( 'ABSPATH' ) ) {
global $product; global $product;
echo apply_filters( 'woocommerce_loop_add_to_cart_link', echo apply_filters( 'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>', sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s" aria-label="%s">%s</a>',
esc_url( $product->add_to_cart_url() ), esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ), esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->get_id() ), esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ), esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ), esc_attr( isset( $class ) ? $class : 'button' ),
esc_attr( $product->add_to_cart_description() ),
esc_html( $product->add_to_cart_text() ) esc_html( $product->add_to_cart_text() )
), ),
$product ); $product );