我想改变产品元标签从“重量”到“平方英尺”在后端和前端。
我试过这个和几百种变体,但都没有成功:
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/
提前谢谢你救了我融化的大脑。:-)
发布于 2020-01-22 17:26:06
你可以用这个片段
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;
}

发布于 2020-01-22 18:41:16
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
});https://stackoverflow.com/questions/59864880
复制相似问题