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

Customize Woocommerce Settings Tab

Let’s talk about customizing Woocommerce Admin setting Tabs.To hide a specific Woocommerce setting tab or want to customize the Woocommerce setting tabs, not the entire sub-menu, but just a tab.For just Removing tabs we can use

How to know “Who created this coupon?”

What to do when your client ask “Who created this coupon?” . in WooCommerce.  But we don’t have any default things in WooCommerce  which automatically show author of coupon .

Top Quick View Plugins WooCommerce

There are several popular WooCommerce quick view plugins that provide a convenient way for customers to view product details without leaving the current page. Keep in mind that the popularity of plugins can change over time, so it’s a good idea to check for the latest information and reviews before making a decision.