使用wc_price过滤器挂钩向产品价格添加其他货币 [英] Adding Additional Currrencies to Product Price using wc_price Filter Hook
本文介绍了使用wc_price过滤器挂钩向产品价格添加其他货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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.
我决定将DIV部分分为四列,以使CSS能够响应.
.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.
这是新代码,给我以下错误:
Parse error: syntax error, unexpected 'return' (T_RETURN)
这是新代码:
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;
}
推荐答案
存在一些错误:
-
您正在与其他字符串一起编写PHP代码,而不使用字符串连接符号
.
($ gbp_price
变量尚未初始化和/或赋值.The
$gbp_price
variable has not been initialized and/or valued.获取美元货币的符号是
USD
,而不是US
.The symbol for getting the dollar currency is
USD
and notUS
.因此正确的功能将是:
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; }
该功能已经过测试,不会返回错误.
这篇关于使用wc_price过滤器挂钩向产品价格添加其他货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文