
7折
减价出售
¥799
在当今全球化的电商环境中,为顾客提供多币种展示功能已成为提升用户体验的关键因素。woocommerce作为最受欢迎的wordpress电商插件,原生支持多币种功能,但需要合理配置才能充分发挥其潜力。通过实现多币种功能,可以实现:
本文将详细介绍从基础配置到高级自定义的完整多币种解决方案,打造真正国际化的在线商店。
// 定义支持的货币和汇率
class WC_Multi_Currency_Manager {
private $currencies = [
'USD' => ['symbol' => '$', 'rate' => 1.00, 'name' => 'US Dollar'],
'EUR' => ['symbol' => '€', 'rate' => 0.85, 'name' => 'Euro'],
'GBP' => ['symbol' => '£', 'rate' => 0.75, 'name' => 'British Pound'],
'JPY' => ['symbol' => '¥', 'rate' => 110.50, 'name' => 'Japanese Yen'],
'CNY' => ['symbol' => '¥', 'rate' => 6.45, 'name' => 'Chinese Yuan']
];
public function __construct() {
add_filter('woocommerce_currencies', [$this, 'add_currencies']);
add_filter('woocommerce_currency_symbol', [$this, 'add_currency_symbol'], 10, 2);
add_action('init', [$this, 'handle_currency_switching']);
}
}
// 货币切换功能实现
class Currency_Switcher {
public function handle_currency_switching() {
if (isset($_GET['currency']) && array_key_exists($_GET['currency'], $this->currencies)) {
$currency = sanitize_text_field($_GET['currency']);
setcookie('wc_selected_currency', $currency, time() + 86400 * 30, COOKIEPATH, COOKIE_DOMAIN);
$_COOKIE['wc_selected_currency'] = $currency;
}
}
public function get_current_currency() {
$default = get_option('woocommerce_currency');
if (isset($_COOKIE['wc_selected_currency']) &&
array_key_exists($_COOKIE['wc_selected_currency'], $this->currencies)) {
return $_COOKIE['wc_selected_currency'];
}
return $default;
}
}
// 货币切换小工具
class Currency_Switcher_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'wc_currency_switcher',
__('货币选择器', 'text_domain'),
['description' => __('允许用户切换显示货币', 'text_domain')]
);
}
public function widget($args, $instance) {
echo $args['before_widget'];
echo $args['before_title'] . __('选择货币', 'text_domain') . $args['after_title'];
$current_currency = $this->get_current_currency();
?>
<form action="" method="GET" class="currency-switcher-form">
<select name="currency">
<?php foreach ($this->currencies as $code => $data): ?>
<option value="<?php echo esc_attr($code); ?>"
<?php selected($current_currency, $code); ?>>
<?php echo esc_html($data['name'] . ' (' . $data['symbol'] . ')'); ?>
</option>
<?php endforeach; ?>
</select>
<?php
// 保留其他GET参数
foreach ($_GET as $key => $value) {
if ($key !== 'currency') {
echo '<input type="hidden" name="' . esc_attr($key) . '" value="' . esc_attr($value) . '">';
}
}
?>
</form>
<?php
echo $args['after_widget'];
}
}
// 前端价格更新脚本
jQuery(document).ready(function($) {
function updatePrices(currency) {
$('.product-price').each(function() {
var basePrice = $(this).data('base-price');
var convertedPrice = basePrice * exchangeRates[currency];
$(this).text(formatCurrency(convertedPrice, currency));
});
}
function formatCurrency(amount, currency) {
var formatter = new Intl.NumberFormat(undefined, {
style: 'currency',
currency: currency,
minimumFractionDigits: 2
});
return formatter.format(amount);
}
$('.currency-switcher select').on('change', function() {
var selectedCurrency = $(this).val();
updatePrices(selectedCurrency);
});
});
// 价格转换核心逻辑
class Price_Converter {
public function __construct() {
add_filter('woocommerce_product_get_price', [$this, 'convert_product_price'], 10, 2);
add_filter('woocommerce_product_variation_get_price', [$this, 'convert_product_price'], 10, 2);
add_filter('woocommerce_variation_prices_price', [$this, 'convert_variation_price'], 10, 3);
}
public function convert_product_price($price, $product) {
$current_currency = $this->get_current_currency();
$base_currency = get_option('woocommerce_currency');
if ($current_currency === $base_currency) {
return $price;
}
return $price * $this->get_exchange_rate($current_currency);
}
private function get_exchange_rate($currency) {
$rates = [
'USD' => 1.0,
'EUR' => 0.85,
'GBP' => 0.75,
'JPY' => 110.50,
'CNY' => 6.45
];
return $rates[$currency] ?? 1.0;
}
}
通过实施上述多币种解决方案,您的WooCommerce商店将具备真正的国际化能力。这种实现方式不仅提供了货币切换功能,还确保了价格计算的准确性和前端展示的一致性。关键优势包括:
为了进一步提升多币种功能的效果,建议考虑:
记住,多币种功能不仅仅是技术实现,更是提升全球客户购物体验的重要策略。通过精心设计和持续优化,您的woocommerce商店将能够更好地服务国际客户,拓展全球市场。
减价出售
减价出售
减价出售
减价出售
电话咨询
1855-626-3292
微信咨询