Search code examples
woocommercecurrency

Adding Additional Currrencies to Product Price using wc_price Filter Hook


Based on the answer from my original post A non well formed numeric value encountered while using wc_price WooCommerce hook, I am now trying to add additional currencies to the function and finally, output them all.

I decided to go with a DIV-section of four columns whereof the CSS makes it responsive.

.exchanged-price{
float: left;
width: 25%;
padding: 15px;
}

.exchange-rate-wrapper:after{
content: "";
display: table;
clear: both;
}

@media screen and (max-width: 900px){
.exchanged-price{
width: 50%;
}}

@media screen and (max-width: 600px){
.exchanged-price{
width: 100%;
}}

I then removed the use of the function, thinking I do not need it.

This is the new code, which gives me the following error:

Parse error: syntax error, unexpected 'return' (T_RETURN)

This is the new code:

add_filter( 'wc_price', 'filter_wc_price', 10, 5 );
function filter_wc_price( $return, $price, $args, $unformatted_price, $original_price = null ) {

    // EUR
    $conversion_rate_eur = (float) 1.25;
    $symbol_eur = 'EUR';
    $currency_symbol_eur = get_woocommerce_currency_symbol( $symbol_eur );
    $euro_price = (float) $price * $conversion_rate_eur;
    // return number_format( $euro_price, 2, '.', '' );

    // US dollar
    $conversion_rate_us = (float) 0.85;
    $symbol_us = 'US';
    $currency_symbol_us = get_woocommerce_currency_symbol( $symbol_us );
    $us_price = (float) $price * $conversion_rate_us;
    // return number_format( $us_price, 2, '.', '' );

    // GP brittish pound
    $conversion_rate_gbp = (float) 1.35;
    $symbol_gbp = 'GBP';
    $currency_symbol_gbp = get_woocommerce_currency_symbol( $symbol_gbp );
    $us_price = (float) $price * $conversion_rate_us;
    // return number_format( $us_price, 2, '.', '' );


    $exchange_rate_section = '
    <div class="exchange-rate-wrapper">
        
        <div class="exchanged-price">
        <h2>' return number_format( $euro_price, 2, '.', '' ); '</h2>
        </div>

        <div class="exchanged-price">
        <h2>'return number_format( $us_price, 2, '.', '' ); '</h2>
        </div>

        <div class="exchanged-price">
        <h2>'return number_format( $gbp_price, 2, '.', '' ); '</h2>
        </div>

        <div class="exchanged-price">
        <h2></h2>
        </div>

    </div>';

    return $return . '<br>' . $exchange_rate_section;
}

Solution

  • There are some errors:

    • You are writing PHP code along with other strings without using the string concatenation symbol . (PHP: String Operators).

    • The $gbp_price variable has not been initialized and/or valued.

    • The symbol for getting the dollar currency is USD and not US.

    So the correct function will be:

    add_filter( 'wc_price', 'filter_wc_price', 10, 5 );
    function filter_wc_price( $return, $price, $args, $unformatted_price, $original_price = null ) {
    
        // EUR
        $conversion_rate_eur = (float) 1.25;
        $symbol_eur = 'EUR';
        $currency_symbol_eur = get_woocommerce_currency_symbol( $symbol_eur );
        $euro_price = (float) $price * $conversion_rate_eur;
        // return number_format( $euro_price, 2, '.', '' );
    
        // US dollar
        $conversion_rate_us = (float) 0.85;
        $symbol_us = 'USD';
        $currency_symbol_us = get_woocommerce_currency_symbol( $symbol_us );
        $us_price = (float) $price * $conversion_rate_us;
        // return number_format( $us_price, 2, '.', '' );
    
        // GP brittish pound
        $conversion_rate_gbp = (float) 1.35;
        $symbol_gbp = 'GBP';
        $currency_symbol_gbp = get_woocommerce_currency_symbol( $symbol_gbp );
        $gbp_price = (float) $price * $conversion_rate_us;
        // return number_format( $gbp_price, 2, '.', '' );
    
    
        $exchange_rate_section = '
        <div class="exchange-rate-wrapper">
            
            <div class="exchanged-price">
            <h2>' . number_format( $euro_price, 2, '.', '' ) . ' ' . $currency_symbol_eur . '</h2>
            </div>
    
            <div class="exchanged-price">
            <h2>'. number_format( $us_price, 2, '.', '' ) . ' ' . $currency_symbol_us . '</h2>
            </div>
    
            <div class="exchanged-price">
            <h2>' . number_format( $gbp_price, 2, '.', '' ) . ' ' . $currency_symbol_gbp . '</h2>
            </div>
    
            <div class="exchanged-price">
            <h2></h2>
            </div>
    
        </div>';
    
        return $return . '<br>' . $exchange_rate_section;
    }
    

    The function has been tested and does not return errors.