在 wooCommerce 的自定义文本区域中保留用户换行符



>我已经设法在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.'');  
}
}
}

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

在可能的情况下(地址中需要其他字段(,我在表单编辑地址中找到了它。

#/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(默认情况下为假(:

#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( '/[rnt ]+/', ' ', $filtered );
}

解决方案 (?

解决方案是基数 - 为所有文本字段留下类似于sanitize_textarea_field ()的换行符:


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

最新更新