WooCommerce Variation Swatches: Making Them Keyboard and Screen-Reader Friendly
Published · 9 min read
WooCommerce's default variation picker is a labeled native <select> for each attribute, and it announces price and stock changes through a live region. That is an accessible baseline. Many swatch plugins hide those selects and replace them with colored div, span or li elements that a keyboard can't reach and a screen reader can't name. The fix is to show a real radio group that drives WooCommerce's hidden select, so everything WooCommerce already does keeps working. This article shows the default markup, the patterns that break it, and child-theme PHP, JavaScript and CSS to put it right. It also covers keeping the radios and the select in sync, price and stock announcements, and testing.
The same principles apply on Shopify. See accessible color swatches for Shopify.
What WooCommerce outputs by default
We checked the classic variable product template (templates/single-product/add-to-cart/variable.php), wc_dropdown_variation_attribute_options() and add-to-cart-variation.js in the WooCommerce 11.2.1 source and the October 2026 development branch. They were the same in both. For a product with a global Color attribute, the form looks like this (trimmed):
<table class="variations" cellspacing="0" role="presentation">
<tbody>
<tr>
<th class="label"><label for="pa_color">Color</label></th>
<td class="value">
<select id="pa_color" name="attribute_pa_color"
data-attribute_name="attribute_pa_color" data-show_option_none="yes">
<option value="">Choose an option</option>
<option value="forest-green">Forest green</option>
<option value="navy">Navy</option>
</select>
<a class="reset_variations" href="#" aria-label="Clear options">Clear</a>
</td>
</tr>
</tbody>
</table>
<div class="reset_variations_alert screen-reader-text" role="alert" aria-live="polite" aria-relevant="all"></div>
<!-- … -->
<div class="woocommerce-variation single_variation" role="alert" aria-relevant="additions"></div>
What this gives you:
- A labeled control per attribute. The
<label for>gives each select an accessible name. The layout table hasrole="presentation", so it isn't announced as a data table. - Unavailable combinations handled in the select. When you choose one attribute,
add-to-cart-variation.jsremoves options for combinations that don't exist and disables options for variations marked inactive. Out-of-stock variations stay selectable unless Hide out of stock items from the catalog is turned on, in which case WooCommerce leaves them out. - Announcements. After a selection, the variation's description, price and availability text ("Out of stock", "3 in stock") are written into
.single_variation, which hasrole="alert". WooCommerce waits 300 ms first so screen readers pick up the change. Invalid combinations get "Sorry, this product is unavailable. Please choose a different combination." The Clear link resets everything and announces "Your selection has been reset…".
A native select isn't perfect. Shoppers can't see the colors without opening it, and the experience depends on your theme's styling. But it works with a keyboard and screen readers out of the box.
Block themes: if your product template uses the Add to Cart + Options (Beta) block, the variation selector is a different component, rendered by WooCommerce Blocks. The PHP filter below targets the classic template, so check which one your product pages use.
What swatch plugins commonly replace it with
Plugins differ, and some do this well. But this pattern is common:
<select id="pa_color" name="attribute_pa_color" style="display: none"> … </select>
<ul class="swatches">
<li class="swatch selected" data-value="forest-green" style="background: #2e5e3b" title="Forest green"></li>
<li class="swatch disabled" data-value="navy" style="background: #1f2a44" title="Navy"></li>
</ul>
What goes wrong:
- No keyboard access. An
liwith a click handler isn't focusable (2.1.1). Addingtabindex="0"without Enter and Space handling doesn't fix it. - No name, role or state. The color name is only in
titleor adata-attribute, and "selected" is a class (4.1.2).aria-labelon an element with no role is not reliably announced. - Color-only meaning. Without text, similar colors can't be told apart, and the tooltip appears only on mouse hover (1.4.1).
- Sold out by fading or a strike. The state is shown visually but not in text.
- Weak selected and focus styles. A thin border in a slightly different color, or
outline: none(2.4.7, 1.4.11). - Small targets. 20 px circles with a 2 px gap (2.5.8).
- ARIA without behavior.
role="radio"with noaria-checkedupdates and no arrow keys promises a control that isn't there.
Patching this markup with ARIA from the outside is fragile, because the plugin redraws it. First check the plugin's settings for an option that keeps the dropdown or shows text labels. If there isn't one, turn off its swatches for that attribute and render your own.
The accessible pattern: a radio group that drives the select
Native radio buttons in a <fieldset> with a <legend> give you one Tab stop per group, arrow keys to change the selection, and a name, role and checked state that every screen reader understands. This matches the WAI-ARIA radio group pattern with no ARIA needed.
The key decision is to keep WooCommerce's select and hide it only once the script has run. WooCommerce's variation script reads the selects to find the variation, submits their values with the form, and writes price and stock into the live region. If the radios set the select's value and fire its change event, all of that keeps working.
The fix in a child theme
Work on a staging copy, in a child theme, so a parent theme update doesn't overwrite your changes. The code below handles one global attribute (pa_color). Change the slug, or loop over several attributes, to suit your store.
1. PHP: add radios after the select
WooCommerce passes the select's HTML through the woocommerce_dropdown_variation_attribute_options_html filter, with the arguments used to build it. Add this to the child theme's functions.php:
<?php
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'child_swatch_radios', 10, 2 );
function child_swatch_radios( $html, $args ) {
$attribute = $args['attribute'];
$product = $args['product'];
if ( 'pa_color' !== $attribute || ! $product instanceof WC_Product || ! taxonomy_exists( $attribute ) ) {
return $html;
}
$options = $args['options'];
if ( empty( $options ) ) {
$all = $product->get_variation_attributes();
$options = isset( $all[ $attribute ] ) ? $all[ $attribute ] : array();
}
$select_id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$selected = sanitize_title( (string) $args['selected'] );
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
$out = '<fieldset class="child-swatches" hidden data-select="' . esc_attr( $select_id ) . '"';
$out .= ' data-sold-out="' . esc_attr__( 'Sold out', 'child-theme' ) . '">';
$out .= '<legend class="child-visually-hidden">' . esc_html( wc_attribute_label( $attribute, $product ) ) . '</legend>';
$out .= '<div class="child-swatches__list">';
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options, true ) ) {
continue;
}
$id = $select_id . '-swatch-' . $term->slug;
$color = sanitize_hex_color( get_term_meta( $term->term_id, 'swatch_color', true ) ); // Example key.
$out .= '<input type="radio" class="child-swatch__input child-visually-hidden" id="' . esc_attr( $id ) . '"';
$out .= ' name="child_swatch_' . esc_attr( $select_id ) . '" value="' . esc_attr( $term->slug ) . '"';
$out .= checked( $selected, $term->slug, false ) . '>';
$out .= '<label class="child-swatch" for="' . esc_attr( $id ) . '">';
if ( $color ) {
$out .= '<span class="child-swatch__chip" style="--chip: ' . esc_attr( $color ) . '"></span>';
}
$out .= '<span class="child-swatch__name">' . esc_html( $term->name ) . '</span>';
$out .= '<span class="child-swatch__status"></span>';
$out .= '</label>';
}
$out .= '</div></fieldset>';
return $html . $out;
}
add_action( 'wp_enqueue_scripts', 'child_swatch_assets' );
function child_swatch_assets() {
if ( ! is_product() ) {
return;
}
$dir = get_stylesheet_directory_uri();
wp_enqueue_style( 'child-swatches', $dir . '/swatches.css', array(), '1.0' );
wp_enqueue_script( 'child-swatches', $dir . '/swatches.js', array( 'jquery', 'wc-add-to-cart-variation' ), '1.0', true );
}
WooCommerce core doesn't store a color for attribute terms. swatch_color is an example meta key: use the key your current plugin stores, or add a color field to the term. If a term has no color, the swatch is a text button, which is still accessible. The color name is visible text. That is the most robust option, and it removes any need for tooltips.
The fieldset starts with hidden, so if the script fails, shoppers still get the normal dropdown. The radios have their own name, so they don't overwrite the attribute_pa_color value WooCommerce reads.
2. JavaScript: keep radios and select in sync
Save as swatches.js in the child theme. WooCommerce's variation script uses jQuery events, and native listeners don't receive events fired with jQuery's .trigger(), so this uses jQuery too.
jQuery( function ( $ ) {
$( '.variations_form' ).each( function () {
var $form = $( this );
var variations = $form.data( 'product_variations' ); // false when variations load by AJAX
// After "Clear", WooCommerce focuses the first select. If that select is hidden, focus the first swatch.
$form.on( 'reset_focus', function () {
if ( $form.find( '.variations select' ).first().is( ':hidden' ) ) {
$form.find( '.child-swatches input:enabled' ).first().trigger( 'focus' );
}
} );
$form.find( '.child-swatches' ).each( function () {
var $group = $( this );
var $select = $form.find( '#' + $group.data( 'select' ) );
var attrName = $select.data( 'attribute_name' );
var $inputs = $group.find( 'input[type="radio"]' );
$group.prop( 'hidden', false );
$select.hide(); // still submitted, still read by WooCommerce
// Radio -> select: WooCommerce listens for this change event.
$inputs.on( 'change', function () {
$select.val( this.value ).trigger( 'change' );
} );
// Select -> radios: after "Clear", or when WooCommerce resets a value.
$select.on( 'change', syncChecked );
$form.on( 'woocommerce_update_variation_values', syncAvailability );
syncAvailability();
function syncChecked() {
var value = $select.val() || '';
$inputs.each( function () {
this.checked = this.value === value;
} );
}
function syncAvailability() {
var chosen = {};
$form.find( '.variations select' ).each( function () {
chosen[ $( this ).data( 'attribute_name' ) ] = $( this ).val() || '';
} );
$inputs.each( function () {
var value = this.value;
var $option = $select.find( 'option' ).filter( function () {
return this.value === value;
} );
var offered = $option.length > 0 && ! $option.prop( 'disabled' );
var soldOut = offered && Array.isArray( variations ) && ! inStock( value, chosen );
this.disabled = ! offered;
$( this ).next( 'label' ).find( '.child-swatch__status' )
.text( soldOut ? $group.data( 'sold-out' ) : '' );
} );
syncChecked();
}
function inStock( value, chosen ) {
return variations.some( function ( variation ) {
for ( var name in variation.attributes ) {
var want = name === attrName ? value : chosen[ name ];
if ( variation.attributes[ name ] && want && variation.attributes[ name ] !== want ) {
return false;
}
}
return variation.is_in_stock;
} );
}
} );
} );
} );
How the sync works:
- Radio to select. Choosing a color sets the select's value and fires
change. WooCommerce then finds the variation and updates price, stock, image and the Add to cart button as usual. - Select to radios. When WooCommerce changes the select itself, for example after Clear or when another choice makes the current color invalid, the radios follow.
- Availability. After WooCommerce rebuilds the options (
woocommerce_update_variation_values), combinations that don't exist are disabled. Disabled radios are skipped by the arrow keys, and screen readers announce them as unavailable, just like disabled options in a select. Colors that exist but are out of stock stay selectable and get visible "Sold out" text. That matches WooCommerce's own behavior, where you can pick an out-of-stock variation and are told it's out of stock. - Focus. The radios are never redrawn, so focus stays on the swatch you chose. After Clear, WooCommerce moves focus to the first select. That select is now hidden, so the script moves focus to the first swatch instead.
Over 30 variations (the default woocommerce_ajax_variation_threshold), WooCommerce loads variation data by AJAX rather than embedding it in the page. The "Sold out" labels are then skipped, and stock is announced after selection.
3. CSS: visible names, clear states, 24 px or more
.child-visually-hidden {
position: absolute !important; width: 1px; height: 1px; margin: -1px; padding: 0;
overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0;
}
.child-swatches { position: relative; min-width: 0; margin: 0; padding: 0; border: 0; }
.child-swatches__list { display: flex; flex-wrap: wrap; gap: 8px; }
.child-swatch {
display: inline-flex; flex-direction: column; align-items: center; gap: 4px;
min-width: 44px; min-height: 44px; padding: 6px 8px;
border: 1px solid #767676; border-radius: 6px; cursor: pointer; line-height: 1.3;
}
.child-swatch__chip {
width: 28px; height: 28px; border-radius: 50%;
background: var(--chip); border: 1px solid #767676; forced-color-adjust: none;
}
.child-swatch__status { font-size: 0.8125rem; }
.child-swatch__status:empty { display: none; }
/* Selected: a thicker, darker border (a change of shape, not just colour) */
.child-swatch__input:checked + .child-swatch { border-color: #121212; box-shadow: inset 0 0 0 2px #121212; }
/* Keyboard focus: a separate ring outside the border */
.child-swatch__input:focus-visible + .child-swatch { outline: 2px solid #121212; outline-offset: 3px; }
/* Combinations that don't exist */
.child-swatch__input:disabled + .child-swatch { opacity: 0.5; cursor: not-allowed; }
#767676 and #121212 give at least 4.5:1 on white. Replace them with your theme's colors and recheck. WCAG doesn't require contrast for disabled controls, which is why the faded style is acceptable there. Sold-out colors are still active, so they get text, not fading.
Announcing price and stock changes
When a shopper picks a color, the new price and stock are status messages: they should be announced without moving focus (4.1.3 Status Messages). WooCommerce already does this through .single_variation. The pattern above keeps it, so in most cases you need nothing more.
It breaks when a theme or plugin hides .single_variation (for example to show the variation price in the main price at the top instead), or removes it with a template override. Content hidden with display: none isn't announced. If that's your setup, add one status region and fill it from WooCommerce's found_variation event:
jQuery( function ( $ ) {
$( '.variations_form' ).each( function () {
var $form = $( this );
var $status = $( '<div class="child-visually-hidden" role="status"></div>' ).appendTo( $form );
$form.on( 'found_variation', function ( event, variation ) {
var $tmp = $( '<div>' ).html( variation.price_html + ' ' + variation.availability_html );
$tmp.find( '[aria-hidden="true"]' ).remove(); // e.g. a crossed-out price that has hidden text instead
$status.text( $.trim( $tmp.text().replace( /\s+/g, ' ' ) ) );
} );
$form.on( 'reset_data', function () {
$status.text( '' );
} );
} );
} );
The region is added when the page loads, before any change, which screen readers need to track it. Our Shopify cart drawer guide follows the same rule for cart updates. Don't add it while .single_variation is still visible, or the price will be read twice. If all variations share a price, WooCommerce leaves the price out of price_html, so only the stock text is announced.
How to test
- Keyboard. Tab to the swatches. Focus should land on the selected color, or the first one if none is selected. Arrow keys change the color, unavailable colors are skipped, and Tab moves on to quantity and Add to cart.
- Screen reader. With NVDA and Firefox or Chrome, and VoiceOver with Safari on iOS, check the group name ("Color"), each color name, "checked", "Sold out", and that the price and stock are announced once.
- Clear. Activate Clear with the keyboard. All radios should be unchecked, a screen reader should announce the reset, and focus should land on the first swatch.
- Form submission. Add each combination to the cart and confirm the right variation arrives.
- Contrast, size and zoom. Check borders and rings for 3:1, targets for 24 px, and wrapping at 200% zoom and 320 px wide.
- Automated checks. An automated check is not an audit. axe-core finds missing names and invalid ARIA, but not whether color alone carries meaning. See what a free scan finds, and what it can't.
Where Sentinela fits
Sentinela's scans run axe-core rules mapped to WCAG 2.2 A/AA plus our own keyboard and purchase-flow checks. SentinelaBot may add a product to the cart with a keyboard, and stops at checkout. An audit adds manual keyboard and screen reader testing of product pages, with a verdict for every WCAG 2.2 A and AA criterion.
For WooCommerce, remediation is done on a staging copy in a child theme, as above, and nothing is published until you approve it. Plugin updates can change swatch markup again, the same kind of regression our Shopify monitoring guide describes. Care Plans include WooCommerce plugin updates and regression checks. For the rest of the purchase flow, see our WooCommerce checkout guide and ten common Shopify and WooCommerce issues.
Sources
- WooCommerce source on GitHub (tag 11.2.1 and trunk, October 2026:
plugins/woocommerce/templates/single-product/add-to-cart/variable.php,templates/single-product/add-to-cart/variation.php,includes/wc-template-functions.php,includes/class-wc-product-variable.php,includes/class-wc-frontend-scripts.php,client/legacy/js/frontend/add-to-cart-variation.js,src/Blocks/BlockTypes/AddToCartWithOptions/) - WooCommerce: Template structure and overriding templates via a theme (vendor)
- WordPress Developer Resources: Child themes
- W3C: Web Content Accessibility Guidelines (WCAG) 2.2
- W3C Understanding documents: 1.4.1, 1.4.11, 2.1.1, 2.4.7, 2.5.8, 4.1.2, 4.1.3
- W3C WAI-ARIA Authoring Practices: Radio Group Pattern
