You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.php file, or use a custom plugin (better to avoid losing code when updating your theme).
  • Tweak the round() precision (the 2 in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:55:26