• Resolved jay126

    (@jay126)


    Hi,

    I trying to customize my account -> edit account page , to add in the profile picture upload function .however the profile picture not able to save into my account.

    function action_woocommerce_edit_account_form() {
        // Get current user id
        $user_id = get_current_user_id();
    
        // 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 );
    
            // Display Image instead of URL
            echo wp_get_attachment_image( $attachment_id, 'full');
        }
    } 
    add_action( 'woocommerce_edit_account_form', 'action_woocommerce_edit_account_form' );
    
    function action_woocommerce_edit_account_form_start() {
        ?>
        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
            <label for="image"><?php esc_html_e( 'Image', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
            <input type="file" class="woocommerce-Input" name="image" accept="image/x-png,image/gif,image/jpeg">
        </p>
        <?php
    }
    add_action( 'woocommerce_edit_account_form_start', 'action_woocommerce_edit_account_form_start' );
    
    // Validate
    function action_woocommerce_save_account_details_errors( $args ){
        if ( isset($_POST['image']) && empty($_POST['image']) ) {
            $args->add( 'image_error', __( 'Please provide a valid image', 'woocommerce' ) );
        }
    }
    add_action( 'woocommerce_save_account_details_errors','action_woocommerce_save_account_details_errors', 10, 1 );
    
    // Save
    function action_woocommerce_save_account_details( $user_id ) {  
        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() );
            } else {
                update_user_meta( $user_id, 'image', $attachment_id );
            }
       }
    }
    add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
    
    // Add enctype to form to allow image upload
    function action_woocommerce_edit_account_form_tag() {
        echo 'enctype="multipart/form-data"';
    } 
    add_action( 'woocommerce_edit_account_form_tag', 'action_woocommerce_edit_account_form_tag' );

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi there ??

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers!

    Thread Starter jay126

    (@jay126)

    Hi folks,

    anyone can help out?

    Thanks

    Plugin Support con

    (@conschneider)

    Engineer

    Howdy,

    The part where the image is suppose to save:

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

    Does this do anything? Or does it fail all together?

    Kind regards,

    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Since it’s been a while since we last heard back from you, I’m going to mark this thread resolved.

    If you have further questions, please feel free to open a new topic.

    Cheers! ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘custom my account to add profile picture’ is closed to new replies.