根据产品价格在 WooCommerce 产品页面上显示其他货币
Displaying Additional Currencies on WooCommerce Product Page based on Product 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 );
我意识到使用 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 );