Home > other >  Save user_description/description of author from my-account/edit-account woocommerce
Save user_description/description of author from my-account/edit-account woocommerce

Time:11-16

I've been trying to save the user_description/description meta of authors from woocommerce's /my-account/edit-account.

I'm using the below code to save other meta. but can't figure out to save the user's bio.

// ================= SAVE ACCOUNT FIELDS / START ==================== //

add_action( 'woocommerce_save_account_details', 'misha_save_account_details' );
function misha_save_account_details( $user_id ) {
    
    if( isset( $_POST[ 'billing_phone' ] ) ){
        update_user_meta( $user_id, 'billing_phone', wc_clean( $_POST[ 'billing_phone' ] ) );
    }
    
    if ( isset( $_FILES['image'] ) ) {
        require_once( ABSPATH . 'wp-admin/includes/image.php' );
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
        require_once( ABSPATH . 'wp-admin/includes/media.php' );

        $attachment_id = media_handle_upload( 'image', 0 );

        if ( is_wp_error( $attachment_id ) ) {
            update_user_meta( $user_id, 'image', $_FILES['image'] . ": " . $attachment_id->get_error_message()[0] );
        } else {
            update_user_meta( $user_id, 'image', $attachment_id );
        }
   }
    
}

// ================= SAVE ACCOUNT FIELDS / END ==================== //

I'm using the below code to display it in there.

// ================= EDIT ACCOUNT FIELDS / START ==================== //

add_action( 'woocommerce_edit_account_form', 'misha_add_field_edit_account_form' );
// or add_action( 'woocommerce_edit_account_form_start', 'misha_add_field_edit_account_form' );
function misha_add_field_edit_account_form() {
    
    // Get current user id
    $user_id = get_current_user_id();
    
    woocommerce_form_field(
        'billing_phone',
        array(
            'type'        => 'tel',
            'required'    => true, // remember, this doesn't make the field required, just adds an "*"
            'label'       => 'Phone number'
        ),
        get_user_meta( $user_id, 'billing_phone', true ) // get the data
    );
    ?>
    
    <p >
        <label for="bio"><?php esc_html_e( 'Bio', 'woocommerce' ); ?>&nbsp;</label>
        <textarea  rows="8" name="user_description"><?= get_the_author_meta( 'user_description', $user_id ); ?></textarea>
    </p>
    
    <?php
    // Get attachment id
    $attachment_id = get_user_meta( $user_id, 'image', true );

    // True
    if ( $attachment_id ) {
        $original_image_url = wp_get_attachment_url( $attachment_id );
        echo wp_get_attachment_image( $attachment_id, 'medium');
    }else {
        echo wp_get_attachment_image(370, 'medium');
    }
    ?>
    <p >
        <label for="image"><?php esc_html_e( 'Image', 'woocommerce' ); ?>&nbsp;<span >*</span></label>
        <input type="file"  name="image" accept="image/x-png,image/gif,image/jpeg">
    </p>
    <?php
}

// ================= EDIT ACCOUNT FIELDS / END ==================== //

I've already thoroughly searched this question on google and StackOverflow. Please don't mark it as duplicate or something. I was wondering why the user_description field is not showing in /edit-account/ by default. So I added it my self woocommerce_edit_account_form but I can't figure out the way to save it.

CodePudding user response:

You can update user description in misha_save_account_details function.

Add this code in misha_save_account_details() function.

if( isset( $_POST[ 'user_description' ] ) ){
    update_user_meta( $user_id, 'description', wc_clean( $_POST[ 'user_description' ] ) );
}
  • Related