How to Make the WooCommerce Sale Price Accessible

How to Make WooCommerce Sale Price Accessible

I recently joined Xero Shoes. They're one of the largest WooCommerce stores in the world. And as luck would have it we received the results of an accessibility (a11y) audit right after I started. That meant my first project is to dive deep into accessibility with WordPress & WooCommerce.

And the first item I looked into was making sure our sale prices were accessible. 👇

How Can a Sale Price Not Be Accessible!?

This is best explained visually. Imagine you are blind and you use screen-reader software to tell you what's on a webpage, what would you expect to hear if you landed on this product page?

A product on XeroShoes.com

I'd expect to hear the product title, the star ratings, the number of reviews, and for me to make a purchase decision about this product I also expect to know that there's a sale going on and how much this product is discounted.

If you take a look at the source-code you'll see some pesky code that's well-meaning but doesn't help blind users:

<del aria-hidden="true">$119.99</del>

The sale price is wrapped in a <del> tag. That tag is the strike-through pricing, but that's actually not the problem. The problem is the aria-hidden="true" attribute.

Aria-hidden tells screen-reading software what to do, and this code tells screen-reading software to ignore this element. So blind users don't know there's a sale going on and they have no idea the current price is actually a discount.

This breaks accessibility guidelines – if you're presenting information to sighted users. You are required to present that same information to screen readers.

Let's Make the WooCommerce Sale Price Accessible

Okay we've defined the problem, so how do we fix this. Well there are two routes:

  1. You can wait. I submitted a pull request to WooCommerce with fixes. That means if they like the way I solve the problem they can merge in my code and release it with the next version of WooCommerce.
  2. You can solve this on your own website (while waiting for WooCommerce). This is what we're doing at Xero Shoes. We want to make WooCommerce better. But we also want to make our site accessible now.

I wrote a single-file utility plugin which solves this in a simple way. You can upload this to your site and screen reading software not only sees the sale price but explains it. 👇

<?php
/*
Plugin Name: WooCommerce Make Sale Prices Accessible
Plugin URI: https://gist.github.com/BFTrick/47b0710a6d27332d0c109cfe75c58be6
Description: Make WooCommerce sale prices accessible
Version: 1.0
Author: Patrick Rauland
Author URI: http://speakinginbytes.com
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: woocommerce-accessible-sale-price
Domain Path: /languages
*/
if ( ! function_exists( 'patricks_format_accessible_sale_price' ) ) {
/**
* WooCommerce sale prices are not accessible by default.
* Until issue 31099 (https://github.com/woocommerce/woocommerce/issues/31099)
* Or PR 44413 (https://github.com/woocommerce/woocommerce/pull/44413) are resolved
* We need to make our own sale prices accessible
*
*/
function patricks_format_accessible_sale_price( $formatted_price, $regular_price, $sale_price ) {
$formatted_price = '<del>
<span class="screen-reader-text">' . esc_html__('Original price was:', 'woocommerce-accessible-sale-price') . '</span> '
. ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) .
'</del>
<ins>
<span class="screen-reader-text">' . esc_html__('Current price is:', 'woocommerce-accessible-sale-price') . '</span> '
. ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) .
'</ins>';
return $formatted_price;
} // end function
}// End if().
add_filter( 'woocommerce_format_sale_price', 'patricks_format_accessible_sale_price', 20, 3 );

So what does this sound like?

Original price was $119.99. Current price is $83.99.

That gives users who use screen reader software context around the sale. They know the original price and can figure out how big of a discount it is. This will help them make a purchase decision.

And it also makes your website accessible. 🙂

Areas for Improvement

I didn't write my code to be robust or work well with other code. If you have another plugin that modifies sale price HTML my code will basically blast it away. You could write more sophisticated code that preserves other logic by looping through each HTML tag and inserting the right content at the right time.

But that's going to take more time to write, and it also takes more PHP processing time when users hit your product page.

Certainly do-able, but we don't have any other plugins that modify the sale price so we don't need our code to be robust.

You can also change my language. Maybe you don't like “Original price was…” and you want to change it to “Full price is…” or something similar. You can do that by changing those lines of text in my plugin before uploading it to your site.

Here's to boosting your sales with accessible pricing! Cheers! 🥂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.