Adding Additional Currrencies to Product Price using wc_price Filter Hook

懵懂的女人 提交于 2021-02-10 14:18:25

问题


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;
}

回答1:


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.



来源:https://stackoverflow.com/questions/66106151/adding-additional-currrencies-to-product-price-using-wc-price-filter-hook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!