skip to Main Content

I’d like to change the product meta label from “Weight” to “Square Feet” in both the back end and front end.

I’ve tried this and a few [hundred] variations without success:

add_filter( 'woocommerce_register_post_type_product', 'custom_product_labels' );

function custom_product_labels( $args ) {
    //
    // change labels in $args['labels'] array
    //
    $args['labels']['_weight'] = 'Square Feet';
    return $args;

I’ve successfully edited the UNITs with this:

add_filter( 'woocommerce_product_settings', 'add_woocommerce_dimension_units' );

function add_woocommerce_dimension_units( $settings ) {
  foreach ( $settings as &$setting ) {

    if ( $setting['id'] == 'woocommerce_dimension_unit' ) {

      $setting['options']['feet'] = __( 'ft' );  // foot
    }

    if ( $setting['id'] == 'woocommerce_weight_unit' ) {

      $setting['options']['sq ft'] = __( 'sq ft' );  // square feet
    }
  }

  return $settings;
}

But I still can’t figure out how to hook into the measurement labels to edit them. It’s important to note that I don’t want to ADD a meta unit of “Square Feet” because we already have thousands of products filled in with the sq ft data in the Weight field.

My quick workaround was to find the actual code on these pages and edit them. But it’s a poor solution.

woocommerce/includes/admin/meta-boxes/views/html-product-data-shipping.php

woocommerce/includes/wc-formatting-functions.php

woocommerce/includes/wc-template-functions.php

Edit: Here is a page showing the use.
https://homedesigningservice.com/product/cape-house-plan-10034-cp/

Thank you in advance for saving my melting brain. 🙂

2

Answers


  1. You may use this snippet

        add_filter( 'gettext', 'theme_change_comment_field_names', 20, 3 );
    
    function theme_change_comment_field_names( $translated_text, $text, $domain ) {
    
                    switch ( $translated_text ) {
    
                        case 'Weight' :
    
                            $translated_text = __( 'Square Feet', $domain );
                            break;
    
                        case 'weight' :
    
                            $translated_text = __( 'Square Feet', $domain );
                            break;
                    }
    
    
                return $translated_text; 
        }
    

    enter image description here

    Login or Signup to reply.
  2. Woo has a filter for front end but no filter for backend label changing.. So use below code and it won’t conflict with any other label… Lakshman’s gettext will change weight anywhere in the site…

    add_filter( 'woocommerce_display_product_attributes', 
     'prefix_change_weight_label_to_square_feet', 10, 2 );
    
     function prefix_change_weight_label_to_square_feet( $product_attributes, $product ) {
    
       // Change Weight to Square Feet
       $product_attributes[ 'weight' ]['label'] = __('Square Feet');
    
       return $product_attributes;
    }
    
    // edit WEIGHT label to SQUARE FEET
    
    add_action( 'admin_footer', function(){
    
       $currentPostType = get_post_type();
    
       if( $currentPostType != 'product' ) return;
    ?>
       <script>
        (function($){
            $(document).ready(function(){
                if ( jQuery('label[for="_weight"]').length ) {
    
                    jQuery('label[for="_weight"]').text("Square Feet");
                }
            });
        })(jQuery);
    </script>
    
     <?php
    
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search