Pawan Kumawat

Upload Any File in My Account Registration Form

To allow users to upload a file in the My Account registration form in WooCommerce, you’ll need to customize the registration form and add a file upload field. We’ll use a custom function and a hook to achieve this. Here’s a step-by-step guide:

1. Add File Upload Field to Registration Form:

Add the following code to your theme’s `functions.php` file or a custom plugin to add a file upload field to the My Account registration form:

function custom_registration_form_file_upload() {
    ?>
    <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
        <label for="file_upload"><?php esc_html_e('Upload Your File', 'text-domain'); ?> <span class="required">*</span></label>
        <input type="file" class="input-file" name="file_upload" id="file_upload" accept=".pdf,.doc,.docx,.png,.jpg,.jpeg" required>
    </p>
    <?php
}
add_action('woocommerce_register_form', 'custom_registration_form_file_upload');

In this code, we added an `<input type=”file”>` field to the registration form with the name attribute set to `file_upload`. The `accept` attribute specifies the file types allowed for upload (you can adjust it according to your requirements).

2. Validate and Process the File Upload:

Next, you’ll need to validate and process the file upload when the user submits the registration form. Use the following code to handle the file upload in the `woocommerce_registration_errors` and `woocommerce_created_customer` hooks:

function custom_registration_form_file_upload_validation($errors, $username, $email) {
    if (isset($_FILES['file_upload']) && !empty($_FILES['file_upload']['name'])) {
        $upload_file = $_FILES['file_upload'];

        // Check for file upload errors
        if ($upload_file['error'] !== UPLOAD_ERR_OK) {
            $errors->add('file_upload_error', __('File upload failed. Please try again.', 'text-domain'));
        }
    }

    return $errors;
}
add_filter('woocommerce_registration_errors', 'custom_registration_form_file_upload_validation', 10, 3);

function custom_registration_form_file_upload_save($customer_id) {
    if (isset($_FILES['file_upload']) && !empty($_FILES['file_upload']['name'])) {
        $upload_file = $_FILES['file_upload'];

        // Handle the file upload and save it to a specific folder
        $upload_dir = wp_upload_dir();
        $file_name = $upload_file['name'];
        $file_tmp = $upload_file['tmp_name'];
        $file_type = wp_check_filetype($file_name);
        $file_path = $upload_dir['path'] . '/' . $file_name;

        // Move the uploaded file to the destination folder
        move_uploaded_file($file_tmp, $file_path);

        // Update user meta with the file path
        update_user_meta($customer_id, 'custom_file_upload', $file_path);
    }
}
add_action('woocommerce_created_customer', 'custom_registration_form_file_upload_save');

 

This code handles the validation and saving of the uploaded file. It checks if the file was uploaded successfully and then moves it to the WordPress uploads folder. It also updates the user meta with the file path for future reference.

Please note that you may need to modify the code to fit your specific requirements, such as the folder where you want to save the uploaded files or the allowed file types.

With these changes, your My Account registration form in WooCommerce will have a file upload field, and the uploaded file will be saved with the user’s account after registration.

Categories

Related Blogs

Create WooCommerce Orders Programmatically

To create WooCommerce orders programmatically in WordPress, you can use the `wc_create_order()` function provided by WooCommerce. This function allows you to create orders and add products to them. Make sure you have WooCommerce installed and activated in your WordPress environment. Here’s a step-by-step guide: