【问题标题】:Preserving the user line breaks in a custom text area in woocommerce在 woocommerce 的自定义文本区域中保留用户换行符
【发布时间】:2020-05-03 12:08:02
【问题描述】:

我已经设法在 woocommerce 产品数据中添加了一个自定义文本区域,我的客户可以在其中添加一个工具包列表,该工具包列表输出到车间的装箱单,但换行符不保存,输出是文本流使得挑选的人不那么容易看 这就是我所拥有的,但我在哪里以及如何让它保留管理套件中添加的换行符以输出到装箱单上:

//General Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields',10,3 
);
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save',10,2 );
//variable data
add_action('woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3);
add_action('woocommerce_save_product_variation','save_variation_settings_fields',10, 2);

//data tab

add_action( 'woocommerce_product_data_panels', 'add_my_custom_product_data_fields',10,3 );
add_action( 'woocommerce_process_product_meta','woocommerce_process_product_meta_fields_save',10,2 );
function woo_add_custom_general_fields() {

global $woocommerce, $post;

// Textarea
woocommerce_wp_textarea_input(
  array(
    'id'          => '_textarea[' . $post->ID . ']',
    'label'       => __( 'Kit List', 'woocommerce' ),
    'placeholder' => '',
    'description' => __( 'Enter the kit list here.', 'woocommerce' ),
    'value'       => get_post_meta( $post->ID, '_textarea', true ),
  )
);
 }

function woo_add_custom_general_fields_save( $post_id ){


// Textarea
$textarea = $_POST['_textarea'][ $post_id ];
if( ! empty( $textarea ) ) {
update_post_meta( $post_id, '_textarea', esc_attr( $textarea ) );
}

}



function variation_settings_fields($loop, $variation_data, $variation){

// Textarea
woocommerce_wp_textarea_input(
  array(
    'id'          => '_textarea[' . $variation->ID . ']',
    'label'       => __( 'Kit List', 'woocommerce' ),
    'placeholder' => '',
    'description' => __( 'Enter the kit list here.', 'woocommerce' ),
    'value'       => get_post_meta( $variation->ID, '_textarea', true ),
  )
);
}
function save_variation_settings_fields($post_id){

// Textarea
 $textarea = $_POST['_textarea'][ $post_id ];
if( ! empty( $textarea ) ) {
update_post_meta( $post_id, '_textarea', esc_attr( $textarea ) );
}


}
add_action( 'wpo_wcpdf_after_item_meta', 'wpo_wcpdf_product_custom_field', 10, 3 );
function wpo_wcpdf_product_custom_field ( $template_type, $item, $order ) {
if ( $template_type == 'packing-slip' ) {
   // check if product exists first
    if (empty($item['product'])) return;

    // replace 'Location' with your custom field name!
    $field_name = '_textarea';
    $textarea = method_exists($item['product'], 'get_meta') ? $item['product']
    ->get_meta($field_name,true,'edit') : get_post_meta( $item['product']->id, $field_name, true );
    if (!empty($textarea)) {
        echo nl2br('Kit List: '.$textarea.'');  
    }
     }
    }

【问题讨论】:

  • 你好丽莎劳顿,欢迎来到 SO!您的问题通常非常详细,嵌入式代码 sn-p 是一个不错的选择 - 但是,对我来说,不清楚您想要的结果是什么样的。能否请您详细说明一下?
  • 您好,我希望 textarea 字段的输出保留添加的换行符 - 文本区域仅在装箱单上输出。基本上,客户使用回车符向 woocommerce 产品数据中的自定义文本区域添加一个列表,并输出为列表格式 - 但是目前它只输出为一长行文本 - 希望这是有道理的
  • 谢谢,我已经阅读了所有这些内容一百万次,并且无法理解将输出包装在
     中的位置,因为我对代码很垃圾,我基本上是从 sn- 拼凑起来的ps 我找到了,直到有些东西起作用。
  • 终于搞定了,换成了 echo '
    Kit List: '.$_textarea.'
    '; with echo nl2br('工具包列表:'.$textarea.'');我已经编辑了上面的代码,以防万一这对其他人有帮助

标签: function textarea line-breaks


【解决方案1】:

函数wc_clean 中的问题 - 它曾经获取值并始终使用_sanitize_text_fields( $str, false ),因此无论字段类型如何,woo 都会删除尾随字符:( 请参见下面的跟踪:

在可能的情况下(Need additional field in address),我在表单编辑地址中找到了它。

#/wp-content/plugins/woocommerce/templates/myaccount/form-edit-address.php:38

woocommerce_form_field( $key, $field, wc_get_post_data_by_key( $key, $field['value'] ) );

其中wc_get_post_data_by_key函数返回wc_clean清理的数据

#/wp-content/plugins/woocommerce/includes/wc-core-functions.php:2005

return wc_clean( wp_unslash( wc_get_var( $_POST[ $key ], $default ) ) ); // @codingStandardsIgnoreLine

使用缎面化文字

#/wp-content/plugins/woocommerce/includes/wc-formatting-functions.php:392

return is_scalar( $var ) ? sanitize_text_field( $var ) : $var;

其中未设置keep_newlines(默认为false):

#WordPress/wp-includes/formatting.php:5244

$filtered = _sanitize_text_fields( $str, false );

因此,函数wc_get_post_data_by_key()接收到的任何类型的所有数据都没有换行符

#WordPress/wp-includes/formatting.php:5317

if ( ! $keep_newlines ) {
    $filtered = preg_replace( '/[\r\n\t ]+/', ' ', $filtered );
}

解决方案(?)

解决方案很重要 - 为所有类似于 sanitize_textarea_field () 的文本字段保留换行符:


add_filter( 'sanitize_text_field',  function ( $filtered, $str ) {
        return _sanitize_text_fields( $str, true );
    }
, 10, 2 );


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2017-03-17
    • 2020-03-15
    相关资源
    最近更新 更多