No matter what you try, but simply adding an HTML input type=”file” won’t work on the WooCommerce Checkout page. I believe this is a security measure and as such, we need to find a workaround.
The only possible solution is to upload the file BEFORE the checkout is submitted, so that upon “Place Order”, the file is already available in the Media section and can be attached to the order as a simple string (URL).
Such upload can happen via Ajax, so that the customer won’t notice anything on the Checkout page – they are actually uploading a file to your website without even noticing it (yes, you need to apply some security measures, of course).
Here’s how it’s done – enjoy!
PHP Snippet: Allow Customers to Upload Files @ WooCommerce Checkout Page
Note 1: in this example, I only allow users to upload a PDF file. This is defined via the accept parameter of the file input type.
Note 2: I use wp_upload_bits() WordPress function to add the file to the current upload directory e.g. /wp-content/uploads/2022/09
Note 3: once the file is uploaded and then attached to the order, I display it to the admin in the Edit Order page and the Admin emails.
/**
* @snippet File Upload Input @ WooCommerce Ccheckout
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @compatible WooCommerce 6
* @community https://businessbloomer.com/club/
*/
add_action( 'woocommerce_after_order_notes', 'bbloomer_checkout_file_upload' );
function bbloomer_checkout_file_upload() {
echo '<p class="form-row"><label for="appform">Application Form (PDF)<abbr class="required" title="required">*</abbr></label><span class="woocommerce-input-wrapper"><input type="file" id="appform" name="appform" accept=".pdf" required><input type="hidden" name="appform_field" /></span></p>';
wc_enqueue_js( "
$( '#appform' ).change( function() {
if ( this.files.length ) {
const file = this.files[0];
const formData = new FormData();
formData.append( 'appform', file );
$.ajax({
url: wc_checkout_params.ajax_url + '?action=appformupload',
type: 'POST',
data: formData,
contentType: false,
enctype: 'multipart/form-data',
processData: false,
success: function ( response ) {
$( 'input[name=\"appform_field\"]' ).val( response );
}
});
}
});
" );
}
add_action( 'wp_ajax_appformupload', 'bbloomer_appformupload' );
add_action( 'wp_ajax_nopriv_appformupload', 'bbloomer_appformupload' );
function bbloomer_appformupload() {
global $wpdb;
$uploads_dir = wp_upload_dir();
if ( isset( $_FILES['appform'] ) ) {
if ( $upload = wp_upload_bits( $_FILES['appform']['name'], null, file_get_contents( $_FILES['appform']['tmp_name'] ) ) ) {
echo $upload['url'];
}
}
die;
}
add_action( 'woocommerce_checkout_process', 'bbloomer_validate_new_checkout_field' );
function bbloomer_validate_new_checkout_field() {
if ( empty( $_POST['appform_field'] ) ) {
wc_add_notice( 'Please upload your Application Form', 'error' );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'bbloomer_save_new_checkout_field' );
function bbloomer_save_new_checkout_field( $order_id ) {
if ( ! empty( $_POST['appform_field'] ) ) {
update_post_meta( $order_id, '_application', $_POST['appform_field'] );
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'bbloomer_show_new_checkout_field_order', 10, 1 );
function bbloomer_show_new_checkout_field_order( $order ) {
$order_id = $order->get_id();
if ( get_post_meta( $order_id, '_application', true ) ) echo '<p><strong>Application PDF:</strong> <a href="' . get_post_meta( $order_id, '_application', true ) . '" target="_blank">' . get_post_meta( $order_id, '_application', true ) . '</a></p>';
}
add_action( 'woocommerce_email_after_order_table', 'bbloomer_show_new_checkout_field_emails', 20, 4 );
function bbloomer_show_new_checkout_field_emails( $order, $sent_to_admin, $plain_text, $email ) {
if ( $sent_to_admin && get_post_meta( $order->get_id(), '_application', true ) ) echo '<p><strong>Application Form:</strong> ' . get_post_meta( $order->get_id(), '_application', true ) . '</p>';
}
I need to exempt the tax if the file is uploaded based on this code. please help
Shahzad, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hey,
This code worked great! Thank you. How would I add 2 upload fields rather than just the one?
Thanks,
Javi
Hey Javi, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
I need to customize this for multiple files input file. Any guides?
Hi Robert, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
How are you Rodolfo Melogli
It Worked but where do I see the link to the image just uploaded?
It said it was successful but the image was never uploaded, am I doing something wrong????
Hello Carlos, thanks so much for your comment! I just retested this on the latest version of WooCommerce and it still works. Unfortunately this looks like custom troubleshooting work and I cannot help here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R
How are you Rodolfo Melogli
How can I make it not to be required*?
PHP Snippet: Allow Customers to Upload Files @ WooCommerce Checkout Page
Sure, you can remove the function bbloomer_validate_new_checkout_field and the hook that triggers that function.
Hello and Thank you for this useful piece of code.
I wanted to change the hook, and add it to woocommerce_review_order_before_submit , to show the upload field right before the Payment button. But when I change the hook it doesn’t work and the error appears.
It may be that it’s messing with the checkout Ajax refresh in there (not 100% sure), and that causes some sort of conflict. Try with other hooks and see if that’s the only one that doesn’t work?
Hi!
Again very thankful for your code, may I get some advice from you!
I’m meeting an issue- the attached file doesn’t show up in the edit order/email notification (even though I can still see it reflect on wp-content/upload).
It is showing an empty image on the edit order, is there some reason it might be the case/setting related?
Thank You So Much!
IL
This should show a link to the image, and not an image. Screenshot please?
Hi!
Appreciate the help so much! How do we limit the size of the upload?
Much Thanks,
IL
Thank you! I believe this should follow your default max upload size, I’m sure there is a way to set a different size in here but I’m afraid this is custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hello! I use your code on my site, this works great, thank you! Can you help me hide this field from non-logged in users?
Hello Andrey, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi.
Could you help me with this:
How I display “Succesfull loaded” confirmation?
How I display the image in checkout, order confirmation and seller email.
How can I have delete option of file uploaded en checkout? This is in case the file were wrong and was necessary re upload
Thank you.
Hello Tracy, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Thank you Rodolfo. I understand.
hi, I want to use this code when a special product add to the card (product id=2299)
so I mix this code with an “if ”
but now, I have problem
when someone adds product id=2299 this work correctly
and when other customers want to buy something else, the brows file doesn’t show but the error happens and says to attach the file
because it’s a required file
tnx
Hello Ehsan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Still works
Great!
Great, but just dont know how to remove that it is not required still?
Simply remove this:
Great Awesome!! Thanks have a nice weekend!
You’re welcome!
No questions – just praise. Thank you for sharing an elegant solution.
Thanks James!