wc_format_sale_price()WC 3.0.0

Format a sale price for display.

Hooks from the function

Return

String.

Usage

wc_format_sale_price( $regular_price, $sale_price );
$regular_price(string) (required)
Regular price.
$sale_price(string) (required)
Sale price.

Changelog

Since 3.0.0 Introduced.

wc_format_sale_price() code WC 9.6.1

function wc_format_sale_price( $regular_price, $sale_price ) {
	// Format the prices.
	$formatted_regular_price = is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price;
	$formatted_sale_price    = is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price;

	// Strikethrough pricing.
	$price = '<del aria-hidden="true">' . $formatted_regular_price . '</del> ';

	// For accessibility (a11y) we'll also display that information to screen readers.
	$price .= '<span class="screen-reader-text">';
	// translators: %s is a product's regular price.
	$price .= esc_html( sprintf( __( 'Original price was: %s.', 'woocommerce' ), wp_strip_all_tags( $formatted_regular_price ) ) );
	$price .= '</span>';

	// Add the sale price.
	$price .= '<ins aria-hidden="true">' . $formatted_sale_price . '</ins>';

	// For accessibility (a11y) we'll also display that information to screen readers.
	$price .= '<span class="screen-reader-text">';
	// translators: %s is a product's current (sale) price.
	$price .= esc_html( sprintf( __( 'Current price is: %s.', 'woocommerce' ), wp_strip_all_tags( $formatted_sale_price ) ) );
	$price .= '</span>';

	return apply_filters( 'woocommerce_format_sale_price', $price, $regular_price, $sale_price );
}