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

问题描述 投票:1回答:1

我设法在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.'');  
    }
     }
    }
function textarea line-breaks
1个回答
0
投票

[C0函数中的麻烦-它曾经获取值并且始终使用wc_clean,所以无论字段类型如何,woo都删除尾随字符:(参见下面的迹线:

在可能的情况下(_sanitize_text_fields( $str, false )),我在表格编辑地址中找到了它。

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; 的地方(默认为false):

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( '/[\r\n\t ]+/', ' ', $filtered ); } 的所有文本字段保留换行符:

sanitize_textarea_field ()
© www.soinside.com 2019 - 2024. All rights reserved.