如何在WooCommerce产品层级管理不同重量单位?
WooCommerce 实现多重量单位自定义输入方案
针对你需要给不同产品设置不同重量单位(Kg、Liter、Pound等)的需求,提供两种可行方案:
方案一:仅自定义重量显示(不参与运费计算)
如果不需要重量参与运费计算,仅需在前台展示自定义的重量文本,可通过以下代码实现:
- 将以下代码添加到主题的
functions.php文件,或使用自定义代码插件(如Code Snippets):
// 在产品编辑页添加自定义重量输入字段 add_action('woocommerce_product_options_general_product_data', 'add_custom_weight_field'); function add_custom_weight_field() { woocommerce_wp_text_input(array( 'id' => '_custom_product_weight', 'label' => __('自定义重量(含单位)', 'woocommerce'), 'placeholder' => '例如:5 Kg、5 Liters、5 Pounds', 'desc_tip' => true, 'description' => '输入产品的重量及对应单位,将替代默认重量显示' )); } // 保存自定义重量字段值 add_action('woocommerce_process_product_meta', 'save_custom_weight_field'); function save_custom_weight_field($post_id) { $custom_weight = isset($_POST['_custom_product_weight']) ? sanitize_text_field($_POST['_custom_product_weight']) : ''; update_post_meta($post_id, '_custom_product_weight', $custom_weight); } // 隐藏默认重量输入框 add_action('admin_head', 'hide_default_weight_field'); function hide_default_weight_field() { echo '<style> .woocommerce_product_options ._weight_field { display: none; } </style>'; } // 替换前台产品页面的重量显示 add_filter('woocommerce_get_product_weight', 'replace_product_weight_display'); function replace_product_weight_display($weight) { global $product; $custom_weight = get_post_meta($product->get_id(), '_custom_product_weight', true); if (!empty($custom_weight)) { return $custom_weight; } // 未填写自定义重量时,显示默认重量 return $weight ? $weight . ' ' . get_option('woocommerce_weight_unit') : ''; }
- 操作步骤:
- 进入产品编辑页,在「常规」选项卡中找到「自定义重量(含单位)」输入框
- 直接输入带单位的文本(如
5 Liters)并保存 - 前台产品页面将自动显示你输入的自定义重量内容
方案二:自定义重量显示+参与运费计算
如果需要重量参与运费计算,同时显示自定义单位,可使用以下代码实现单位自动转换:
- 添加代码到主题
functions.php或自定义代码插件:
// 在产品编辑页添加带单位的重量输入框,替换默认字段 add_action('woocommerce_product_options_general_product_data', 'add_weight_with_unit_field'); function add_weight_with_unit_field() { global $post; $product = wc_get_product($post->ID); $default_unit = get_option('woocommerce_weight_unit'); // 读取已保存的带单位重量,无数据则用默认单位显示现有重量 $weight_with_unit = get_post_meta($post->ID, '_weight_with_unit', true); if (empty($weight_with_unit) && $product->get_weight()) { $weight_with_unit = $product->get_weight() . ' ' . $default_unit; } woocommerce_wp_text_input(array( 'id' => '_weight_with_unit', 'label' => __('重量(含单位)', 'woocommerce'), 'placeholder' => '例如:5 Kg、5 Liters、5 Pounds', 'desc_tip' => true, 'description' => '支持单位:Kg, g, Pound, oz, Liter(Liter按1L=1Kg计算运费,可自行调整转换规则)', 'value' => $weight_with_unit )); // 隐藏默认重量输入框 echo '<style>.woocommerce_product_options ._weight_field { display: none; }</style>'; } // 保存时自动转换为默认单位,用于运费计算 add_action('woocommerce_process_product_meta', 'save_weight_with_unit'); function save_weight_with_unit($post_id) { if (!isset($_POST['_weight_with_unit'])) return; $weight_input = sanitize_text_field($_POST['_weight_with_unit']); // 拆分数值与单位 preg_match('/^(\d+\.?\d*)\s*([a-zA-Z]+)$/', $weight_input, $matches); if (count($matches) != 3) { // 格式错误时清空重量数据 update_post_meta($post_id, '_weight_with_unit', ''); update_post_meta($post_id, '_weight', ''); return; } $numeric_weight = (float)$matches[1]; $unit = strtolower($matches[2]); $default_unit = strtolower(get_option('woocommerce_weight_unit')); // 单位转换规则,可根据需求修改 $conversion_rates = array( 'kg' => 1, 'g' => 0.001, 'pound' => 0.453592, 'oz' => 0.0283495, 'liter' => 1 // 1升=1公斤,可自行调整 ); if (!isset($conversion_rates[$unit])) { // 不支持的单位,清空数据 update_post_meta($post_id, '_weight_with_unit', ''); update_post_meta($post_id, '_weight', ''); return; } // 转换为默认单位的重量值 $converted_weight = $numeric_weight * $conversion_rates[$unit]; if ($default_unit != 'kg') { $converted_weight = $converted_weight / $conversion_rates[$default_unit]; } // 保存自定义带单位文本和转换后的数值 update_post_meta($post_id, '_weight_with_unit', $weight_input); update_post_meta($post_id, '_weight', number_format($converted_weight, 4)); } // 前台显示自定义带单位的重量 add_filter('woocommerce_get_product_weight', 'display_weight_with_custom_unit'); function display_weight_with_custom_unit($weight) { global $product; $weight_with_unit = get_post_meta($product->get_id(), '_weight_with_unit', true); if (!empty($weight_with_unit)) { return $weight_with_unit; } // 未填写时回退到默认显示 return $weight ? $weight . ' ' . get_option('woocommerce_weight_unit') : ''; }
- 操作说明:
- 产品编辑页输入带单位的重量(如
5 Pounds),系统会自动转换为你WooCommerce设置的默认重量单位(如Kg)用于运费计算 - 前台依然显示你输入的
5 Pounds文本 - 若需要调整单位转换规则,可修改代码中的
$conversion_rates数组
插件方案(可选)
如果不想编写代码,可搜索安装WooCommerce第三方插件,如WooCommerce Custom Weight Units,这类插件支持为单个产品设置自定义重量单位。
内容的提问来源于stack exchange,提问作者MD JAHID HASAN
相关产品推荐
相关产品推荐

