phpwoocommercefilterdecimal

Stylization of decimals as uppercase in WooCommerce only in Single Product and Product Archive Pages


I'd like your help for a specific case. I was able to make the stylization of the price decimals exactly as I wished on my WooCommerce store.

However, the problem is that now this code also applies for my WooCommerce checkout and emails. Do you know how I could make that the below code do not impact the WooCommerce checkout and emails pages and only on Single Product and Product Archive Pages? What is the most suitable filter to apply?

add_filter( 'formatted_woocommerce_price', 'ts_woo_decimal_price', 10, 5 );
function ts_woo_decimal_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
    // Not on backend
    if ( ! is_admin() ) {
        $price_data = explode($decimal_separator, $formatted_price);
        return $price_data[0] . '<sup>' . $price_data[1] . '</sup>';
    }
    return $formatted_price;
}

What is the most suitable filter to apply?


Solution

  • To target shop pages, taxonomy archive pages and product single pages, use inside your function:

    if ( is_shop() || is_tax() || is_product() ) {
        // Your active code
    }
    

    It should work.