我意识到使用
wc_price
并不是一个好方法,因为它会影响系统中的其他所有内容。因此,我希望仅在产品页面上实现此功能,而不影响购物车、结账、迷你购物车、管理订单和其他所有内容...
因此,基于使用 wc_price 过滤器钩子向产品价格添加其他货币 - 这是我实现这一目标的尝试。问题是;什么也没有显示。
add_filter( 'woocommerce_single_product_summary', 'manual_currency_converter', 5, 1);
function manual_currency_converter($price) {
$product = wc_get_product();
$product_price = $product->get_price();
// EUR
$conversion_rate_eur = (float) 1.25;
$symbol_eur = 'EUR';
$currency_symbol_eur = get_woocommerce_currency_symbol($symbol_eur);
$euro_price = (float) $product_price * $conversion_rate_eur;
// US dollar
$conversion_rate_us = (float) 0.85;
$symbol_us = 'USD';
$currency_symbol_us = get_woocommerce_currency_symbol($symbol_us);
$us_price = (float) $product_price * $conversion_rate_us;
// GP brittish pound
$conversion_rate_gbp = (float) 1.35;
$symbol_gbp = 'GBP';
$currency_symbol_gbp = get_woocommerce_currency_symbol($symbol_gbp);
$gbp_price = (float) $product_price * $conversion_rate_us;
$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>';
return $price . '<br>' . $exchange_rate_section;
}
您仍然可以使用
wc_price
过滤器挂钩,如果您希望代码仅在单个产品页面上运行,则在函数开头使用 is_product()
所以你得到:
function filter_wc_price( $return, $price, $args, $unformatted_price, $original_price = null ) {
// NOT true on a single product page, RETURN
if ( ! is_product() ) return $return;
// else continue.. my other code..
return $return;
}
add_filter( 'wc_price', 'filter_wc_price', 10, 5 );