【发布时间】: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