我意识到使用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 );
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句