【问题标题】:Customizing WooCommerce product data label without plugin - WEIGHT在没有插件的情况下自定义 WooCommerce 产品数据标签 - WEIGHT
【发布时间】:2020-01-22 17:07:50
【问题描述】:

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

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

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;

我已经成功地用这个编辑了 UNIT:

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/

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

【问题讨论】:

    标签: php wordpress woocommerce label product


    【解决方案1】:

    你可以使用这个sn-p

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

    【讨论】:

    • 嗯。那没有用。我认为这不是问题,但我正在使用 WOOF Products Filter 插件。它唯一影响的(据我所知)是此处主商店页面上滑块过滤器的标签:homedesigningservice.com/product-category/house-plans
    • 所以我假设有一些其他插件导致这个运行正常的问题。关于如何开始搜索有什么想法吗?
    • 如果添加静态,您可以使用字符串定位器插件在主题或插件中查找“重量”字
    • 更改优先级然后检查
    • 20 替换为 99
    【解决方案2】:

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

    【讨论】:

    • 我猜它不适用于订单页面和订单发票?
    • 如果需要...可以通过过滤器更改...我们也有过滤器...
    猜你喜欢
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 2015-07-20
    • 2014-07-16
    • 2014-09-24
    • 2021-01-16
    相关资源
    最近更新 更多