WooCommerce商品页附加信息栏修改及单位转换需求咨询
Absolutely, both of your requests are totally doable with custom code snippets for WooCommerce—no need to edit core files (which is always a good practice to keep things update-safe). Let’s walk through each task:
WooCommerce has a built-in filter that lets you override the dimensions label. Just add this code to your theme’s functions.php file, or use a custom code plugin (like Code Snippets) to avoid losing changes when you update your theme:
add_filter( 'woocommerce_product_dimensions_label', 'change_dimensions_label_to_box_dimensions' ); function change_dimensions_label_to_box_dimensions() { return __( 'Box Dimensions', 'woocommerce' ); }
This hooks into the label output and replaces the default "Dimensions" text with "Box Dimensions" across all product pages.
To convert the stored gram weight to kilograms for front-end display, use the woocommerce_get_product_weight filter. This snippet will take the gram value, divide by 1000, format it nicely, and append the KG unit:
add_filter( 'woocommerce_get_product_weight', 'convert_grams_to_kg_for_display', 10, 2 ); function convert_grams_to_kg_for_display( $weight, $product ) { // Check if the weight exists and your store's base unit is grams if ( ! empty( $weight ) && get_option( 'woocommerce_weight_unit' ) === 'g' ) { // Convert grams to kg, round to 2 decimal places for readability $kg_weight = round( $weight / 1000, 2 ); // Return the formatted weight with KG unit return $kg_weight . ' KG'; } // Fallback to original weight if conditions aren't met return $weight; }
Quick check: Make sure your WooCommerce store’s base weight unit is set to grams (you can verify this in WooCommerce > Settings > Products > General > Weight Unit). If it’s set to something else, tweak the condition in the code to match your setup.
Both snippets will work seamlessly in the Additional Information tab, as well as any other spots where product dimensions/weight show up on your site.
内容的提问来源于stack exchange,提问作者Jonny B'Good

