You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce产品单页第二个自定义文本域无法保存求助

WooCommerce自定义文本域无法保存的问题排查与修复

你的第二个文本域_textarea_materiaux无法保存输入内容,核心原因是保存函数里的变量名拼写错误,具体问题如下:

  • 接收POST值时,变量名多写了一个下划线:$woocommerce_textarea__materiaux(正确应为$woocommerce_textarea_materiaux)
  • 调用update_post_meta时,传入的变量$woocommerce_textarea_materiaux未被定义,导致没有实际值写入数据库

修正后的完整代码如下:

/*custom field*/
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

function woo_add_custom_general_fields() {
  global $woocommerce, $post;
  echo '<div class="options_group">';
  // Custom fields will be created here...
        
        // Taille et coupe
        woocommerce_wp_textarea_input( 
            array( 
                'id'          => '_textarea_taille_coupe', 
                'label'       => __( 'Taille et coupe', 'woocommerce' ), 
                'placeholder' => '', 
                'description' => __( 'Taille et coupe', 'woocommerce' ) 
            )
        );
    
        // Matériaux et entretien
        woocommerce_wp_textarea_input( 
                array( 
                    'id'          => '_textarea_materiaux', 
                    'label'       => __( 'Matériaux et entretien', 'woocommerce' ), 
                    'placeholder' => '', 
                    'description' => __( 'materiaux et entretien', 'woocommerce' ) 
                )
            );
        
}

function woo_add_custom_general_fields_save( $post_id ){
    
    // Textarea
    $woocommerce_textarea_taille_coupe = $_POST['_textarea_taille_coupe'];
    if( isset( $woocommerce_textarea_taille_coupe ) )
        update_post_meta( $post_id, '_textarea_taille_coupe',  $woocommerce_textarea_taille_coupe  );
    
    // Textarea - 修正变量名错误
    $woocommerce_textarea_materiaux = $_POST['_textarea_materiaux'];
    if( isset( $woocommerce_textarea_materiaux) )
        update_post_meta( $post_id, '_textarea_materiaux',  $woocommerce_textarea_materiaux  );
    
}

内容的提问来源于stack exchange,提问作者StudentDevelopper12120

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 21:25:37