Mohamed
Mohamed

Reputation: 47

Replace specific currency symbol on all WooCommerce HTML formatted prices?

I am trying to add svg image or png image on the wordpress it work everywhere except in:

It parse that as escaped HTML code, so the display in cart is:

1.000,00 <span class="sar-svg-symbol"><img src="http://localhost:8888/SRS/wp-content/uploads/2025/02/sar_svg_symbol.png" alt="SAR Symbol" style="display:inline-block; width:20px; height:auto; vertical-align:middle;"></span>

This is my code

function sar_svg_force_png_in_cart_and_checkout($price, $cart_item = null, $cart_item_key = null) {
    $png_url = get_option('sar_svg_png_url', '');

    if (empty($png_url)) {
        return $price; // Return original price if PNG is missing
    }

    // Remove any existing currency symbols (SAR or ر.س) to prevent duplication
    $price = preg_replace('/(\s?SAR|\s?ر\.س|\s?ريال)/u', '', $price);

    // Ensure PNG is not duplicated
    if (strpos($price, 'sar_svg_symbol.png') !== false) {
        return $price;
    }

    // Ensure WooCommerce renders the PNG image
    $formatted_price = trim($price) . ' <span class="sar-svg-symbol"><img src="' . esc_url($png_url) . '" alt="SAR Symbol" style="display:inline-block; width:20px; height:auto; vertical-align:middle;"></span>';
    
    return wp_kses_post($formatted_price);
}

// ✅ Apply fix to Cart, Checkout, and Order Totals
add_filter('woocommerce_cart_item_price', 'sar_svg_force_png_in_cart_and_checkout', PHP_INT_MAX, 3);
add_filter('woocommerce_cart_item_subtotal', 'sar_svg_force_png_in_cart_and_checkout', PHP_INT_MAX, 3);
add_filter('woocommerce_cart_totals_order_total_html', 'sar_svg_force_png_in_cart_and_checkout', PHP_INT_MAX, 3);
add_filter('woocommerce_order_formatted_line_subtotal', 'sar_svg_force_png_in_cart_and_checkout', PHP_INT_MAX, 3);
add_filter('woocommerce_email_order_item_subtotal', 'sar_svg_force_png_in_cart_and_checkout', PHP_INT_MAX, 3);
add_filter('woocommerce_email_order_total', 'sar_svg_force_png_in_cart_and_checkout', PHP_INT_MAX, 3);

Upvotes: 2

Views: 69

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254363

WooCommerce uses the function get_woocommerce_currency_symbol() to get the current currency symbol, so you can use woocommerce_currency_symbol filter hook to change the currency symbol for "SAR" currency.

Also, WooCommerce uses the function wc_price() to format all displayed prices. Then you can use wc_price available filter hook to make changes to the price HTML output.

Try to replace your current related code with:

// Alter WooCommerce currency symbol for "SAR" currency
add_filter( 'woocommerce_currency_symbol', 'sar_currency_symbol', 20, 2 );
function sar_currency_symbol( $currency_symbol, $currency ) {
    // NOT in Admin WooCommerce general settings
    if ( is_admin() && isset($_GET['page']) && 'wc-settings' === $_GET['page'] 
    && isset($_GET['tab']) && 'general' === $_GET['tab'] ) {
        return $currency_symbol;
    }

    $sar_src = get_option('sar_svg_png_url');

    if ( $currency === 'SAR' && $sar_src ) {
        return sprintf('<img src="%s" alt="SAR Symbol" style="display:inline-block; width:20px; height:auto; vertical-align:middle;">', 
        esc_url( $sar_src ) ); 
    }
    return $currency_symbol;
}

// Alter WooCommerce price HTML output for "SAR" currency (optional)
add_filter( 'wc_price', 'sar_currency_symbol_price_html', 20, 3 );
function sar_currency_symbol_price_html( $price_html, $price, $args ) {
    $sar_src = get_option('sar_svg_png_url');

    if ( $args['currency'] === 'SAR' && $sar_src ) {
        // Replace WC default currency html "class" attribute for "SAR" currency
        $price_html = str_replace('woocommerce-Price-currencySymbol', 'sar-svg-symbol', $price_html); 
    }
    return $price_html;
}

It will handle the correct currency symbol HTML output everywhere for "SAR" currency.

For example, in cart:

Cart page

Upvotes: 1

Related Questions