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

Commit

Permalink
Ensure aria-label is showing correct value based on setting (#9672)
Browse files Browse the repository at this point in the history
* Ensure aria-label is showing correct value based on setting

* Reuse same format code
  • Loading branch information
roykho authored Jun 1, 2023
1 parent fcf200e commit 421ab62
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 27 deletions.
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
49 changes: 33 additions & 16 deletions assets/js/blocks/mini-cart/utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,47 @@ export const updateTotals = ( totals: [ string, number ] | undefined ) => {
return;
}
const [ amount, quantity ] = totals;
const miniCartButtons = document.querySelectorAll(
'.wc-block-mini-cart__button'
);
const miniCartBlocks = document.querySelectorAll( '.wc-block-mini-cart' );
const miniCartQuantities = document.querySelectorAll(
'.wc-block-mini-cart__badge'
);
const miniCartAmounts = document.querySelectorAll(
'.wc-block-mini-cart__amount'
);

miniCartButtons.forEach( ( miniCartButton ) => {
miniCartButton.setAttribute(
miniCartBlocks.forEach( ( miniCartBlock ) => {
if ( ! ( miniCartBlock instanceof HTMLElement ) ) {
return;
}

const miniCartButton = miniCartBlock.querySelector(
'.wc-block-mini-cart__button'
);

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',
quantity,
'woo-gutenberg-products-block'
),
quantity,
amount
)
miniCartBlock.dataset.hasHiddenPrice
? 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
)
: 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'
),
quantity,
amount
)
);
} );
miniCartQuantities.forEach( ( miniCartQuantity ) => {
Expand Down

0 comments on commit 421ab62

Please sign in to comment.