繁体   English   中英

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

[英]Customizing WooCommerce product data label without plugin - WEIGHT

我想将后端和前端的产品元标签从“重量”更改为“平方英尺”。

我已经尝试过这个和几个 [数百] 变体但没有成功:

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;

我已经成功地编辑了单位:

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

但我仍然无法弄清楚如何挂钩测量标签来编辑它们。 重要的是要注意,我不想添加“平方英尺”的元单位,因为我们已经在重量字段中用平方英尺数据填充了数千种产品。

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

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

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM