Multiple custom field text wordpress for a single page product

i have done this to have some custom field on my product single page

here is my the code

/*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  );
    
}

The first textaera is working (add in front office - save in back office ) However the second one is not working (_textaera_materiaux).

I don't understand why the first one is working with the same code and the second one i can't save what i am typing.

Thanks for your helps guys.

Upvotes: 1

Views: 132

Answers (1)

Krunal Bhimajiyani
Krunal Bhimajiyani

Reputation: 1179

Your variable name is different in update_post_meta.

Updated code:

$woocommerce_textarea__materiaux   = $_POST['_textarea_materiaux'];
if ( isset( $woocommerce_textarea__materiaux ) ) {
    update_post_meta( $post_id, '_textarea_materiaux', $woocommerce_textarea__materiaux );
}

In the update post meta, you wrote $woocommerce_textarea_materiaux. But actually, you declared a double underscore in the variable name.

Upvotes: 1

Related Questions