mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-29 11:32:21 +08:00
https://stackoverflow.com/questions/76748046/replace-woocommerce-0-sale-price-with-a-custom-text-keeping-regular-price-strike
10 lines
637 B
Text
10 lines
637 B
Text
add_filter( 'woocommerce_get_price_html', 'custom_formatted_sale_price_html', 10, 2 );
|
|
function custom_formatted_sale_price_html( $price_html, $product ) {
|
|
if ( $product->is_on_sale() && $product->get_price() == 0 ) {
|
|
$regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
|
|
$sale_price_text = $regular_price > 0 ? __( 'Free only for members!', 'woocommerce' ) : __( 'Free!', 'woocommerce' );
|
|
|
|
return '<del aria-hidden="true">' . wc_price( $regular_price ) . '</del> <span class="sale-text">' . $sale_price_text . '</span>';
|
|
}
|
|
return $price_html;
|
|
}
|