Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Ensure aria-label is showing correct value based on setting #9672

Merged
merged 2 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions assets/js/blocks/mini-cart/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,31 @@ const MiniCartBlock = ( attributes: Props ): JSX.Element => {
parseInt( cartTotals.total_items_tax, 10 )
: parseInt( cartTotals.total_items, 10 );

const ariaLabel = sprintf(
/* translators: %1$d is the number of products in the cart. %2$s is the cart total */
_n(
'%1$d item in cart, total price of %2$s',
'%1$d items in cart, total price of %2$s',
cartItemsCount,
'woo-gutenberg-products-block'
),
cartItemsCount,
formatPrice( subTotal, getCurrencyFromPriceResponse( cartTotals ) )
);
const ariaLabel = hasHiddenPrice
? sprintf(
/* translators: %1$d is the number of products in the cart. */
_n(
'%1$d item in cart',
'%1$d items in cart',
cartItemsCount,
'woo-gutenberg-products-block'
),
cartItemsCount
)
: sprintf(
/* translators: %1$d is the number of products in the cart. %2$s is the cart total */
_n(
'%1$d item in cart, total price of %2$s',
'%1$d items in cart, total price of %2$s',
cartItemsCount,
'woo-gutenberg-products-block'
),
cartItemsCount,
formatPrice(
subTotal,
getCurrencyFromPriceResponse( cartTotals )
)
);

const colorStyle = {
backgroundColor: style?.color?.background,
Expand Down
51 changes: 35 additions & 16 deletions assets/js/blocks/mini-cart/utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export const updateTotals = ( totals: [ string, number ] | undefined ) => {
return;
}
const [ amount, quantity ] = totals;
const miniCartButtons = document.querySelectorAll(
'.wc-block-mini-cart__button'
const miniCarts = document.querySelectorAll(
'.wc-block-mini-cart.wp-block-woocommerce-mini-cart'
roykho marked this conversation as resolved.
Show resolved Hide resolved
);
const miniCartQuantities = document.querySelectorAll(
'.wc-block-mini-cart__badge'
Expand All @@ -23,21 +23,40 @@ export const updateTotals = ( totals: [ string, number ] | undefined ) => {
'.wc-block-mini-cart__amount'
);

miniCartButtons.forEach( ( miniCartButton ) => {
miniCartButton.setAttribute(
'aria-label',
sprintf(
/* translators: %s number of products in cart. */
_n(
'%1$d item in cart, total price of %2$s',
'%1$d items in cart, total price of %2$s',
miniCarts.forEach( ( miniCart ) => {
roykho marked this conversation as resolved.
Show resolved Hide resolved
const hasHiddenPrice = miniCart.getAttribute( 'data-has-hidden-price' );
roykho marked this conversation as resolved.
Show resolved Hide resolved
const button = miniCart.querySelector( '.wc-block-mini-cart__button' );
roykho marked this conversation as resolved.
Show resolved Hide resolved

if ( hasHiddenPrice ) {
button?.setAttribute(
'aria-label',
sprintf(
/* translators: %s number of products in cart. */
_n(
'%1$d item in cart',
'%1$d items in cart',
quantity,
'woo-gutenberg-products-block'
),
quantity
)
);
} else {
button?.setAttribute(
'aria-label',
sprintf(
/* translators: %1$d is the number of products in the cart. %2$s is the cart total */
_n(
'%1$d item in cart, total price of %2$s',
'%1$d items in cart, total price of %2$s',
quantity,
'woo-gutenberg-products-block'
),
roykho marked this conversation as resolved.
Show resolved Hide resolved
quantity,
'woo-gutenberg-products-block'
),
quantity,
amount
)
);
amount
)
);
}
roykho marked this conversation as resolved.
Show resolved Hide resolved
} );
miniCartQuantities.forEach( ( miniCartQuantity ) => {
if ( quantity > 0 || miniCartQuantity.textContent !== '' ) {
Expand Down