无需插件即可自定义 WooCommerce 产品数据标签 - WEIGHT

Customizing WooCommerce product data label without plugin - WEIGHT

我想将后端和前端的产品元标签从 "Weight" 更改为 "Square Feet"。

我试过这个和几个 [00] 个变体但没有成功:

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;

我已经成功编辑了 UNITs:

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;
}

但我仍然不知道如何连接到测量标签来编辑它们。 请务必注意,我不想添加 "Square Feet" 的元单位,因为我们已经有数千种产品在“重量”字段中填写了平方英尺数据。

我的快速解决方法是找到这些页面上的实际代码并进行编辑。但这是一个糟糕的解决方案。

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

woocommerce/includes/wc-formatting-functions.php

woocommerce/includes/wc-template-functions.php

编辑:这是一个显示使用的页面。 https://homedesigningservice.com/product/cape-house-plan-10034-cp/

提前感谢您拯救了我正在融化的大脑。 :-)

您可以使用此代码段

    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; 
    }

Woo 有一个前端过滤器,但没有后端标签更改过滤器。所以使用下面的代码,它不会与任何其他标签冲突...Lakshman 的 gettext 将在站点的任何位置更改权重...

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

});