WooCommerce配置:美国客户组登录显示英制单位,后台保留公制
Absolutely! You can totally pull this off without touching your core database values (keeping kg/cm as the primary units). The trick is to dynamically convert and display imperial units (pounds/inches) only for users in the US-Client group while leaving your backend data completely untouched. Here's how to make it work:
1. First, Detect the US-Client User Group
We’ll start with a helper function to check if the logged-in user belongs to your target group. Just make sure to swap us-client with your actual user group slug if it’s different:
function is_us_client_user() { $current_user = wp_get_current_user(); if (is_user_logged_in() && in_array('us-client', $current_user->roles)) { return true; } return false; }
2. Convert Weight Units (kg ↔ lbs)
Use WooCommerce’s woocommerce_format_weight filter to override how weight is displayed for US clients. You have two options here: replace metric entirely, or show both units side-by-side.
Option A: Replace with Imperial Only
function us_client_convert_weight($weight_display, $weight, $unit) { if (is_us_client_user() && $unit === 'kg') { // Convert kg to lbs (1kg ≈ 2.20462 lbs) $lbs_weight = round($weight * 2.20462, 2); return $lbs_weight . ' lb'; } return $weight_display; } add_filter('woocommerce_format_weight', 'us_client_convert_weight', 10, 3);
Option B: Show Both Metric and Imperial
function us_client_display_dual_weight($weight_display, $weight, $unit) { if (is_us_client_user() && $unit === 'kg') { $lbs_weight = round($weight * 2.20462, 2); return $weight_display . ' (' . $lbs_weight . ' lb)'; } return $weight_display; } add_filter('woocommerce_format_weight', 'us_client_display_dual_weight', 10, 3);
3. Convert Dimension Units (cm ↔ inches)
For product length/width/height, use the woocommerce_format_dimensions filter to adjust the display similarly.
Option A: Replace with Imperial Only
function us_client_convert_dimensions($dimension_display, $dimensions) { if (!is_us_client_user()) return $dimension_display; $unit = get_option('woocommerce_dimension_unit'); if ($unit !== 'cm') return $dimension_display; // Convert each cm value to inches (1cm ≈ 0.393701 inches) $inches = array_map(function($cm) { return round($cm * 0.393701, 2); }, $dimensions); return implode(' × ', $inches) . ' in'; } add_filter('woocommerce_format_dimensions', 'us_client_convert_dimensions', 10, 2);
Option B: Show Both Metric and Imperial
function us_client_display_dual_dimensions($dimension_display, $dimensions) { if (!is_us_client_user()) return $dimension_display; $unit = get_option('woocommerce_dimension_unit'); if ($unit !== 'cm') return $dimension_display; $inches = array_map(function($cm) { return round($cm * 0.393701, 2); }, $dimensions); $imperial_display = implode(' × ', $inches) . ' in'; return $dimension_display . ' (' . $imperial_display . ')'; } add_filter('woocommerce_format_dimensions', 'us_client_display_dual_dimensions', 10, 2);
4. Keep Cart/Checkout Consistent
Don’t let the cart or checkout pages break the experience—update weight displays there too:
// Adjust cart item weight display function us_client_cart_item_weight($weight, $cart_item) { if (is_us_client_user() && $cart_item['data']->get_weight_unit() === 'kg') { $lbs_weight = round($cart_item['data']->get_weight() * 2.20462, 2); return $lbs_weight . ' lb'; } return $weight; } add_filter('woocommerce_cart_item_weight', 'us_client_cart_item_weight', 10, 2);
Quick Tips
- Add these snippets to your theme’s
functions.phpfile, or use a custom plugin (better to avoid losing code when updating your theme). - Tweak the
round()precision (the2in the examples) to match how precise you want the imperial values to be. - If you need shipping calculations to use imperial units, you’ll need to add extra hooks to convert weights during shipping quote generation—let me know if you need help with that!
This setup keeps your database clean (all values stay in kg/cm) while giving US clients a familiar, localized shopping experience.
内容的提问来源于stack exchange,提问作者James Donald

