WooCommerce: Upload File @ Checkout Page

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!

Thanks to the snippet below, I was able to display a file upload field on the WooCommerce Checkout page, attach this to the order, and return it on the admin screens / emails.

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>';
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]
  • WooCommerce: Disable Payment Method If Product Category @ Cart
    Today we take a look at the WooCommerce Checkout and specifically at how to disable a payment gateway (e.g. PayPal) if a specific product category is in the Cart. There are two tasks to code in this case: (1) based on all the products in the Cart, calculate the list of product categories in the […]
  • WooCommerce: Add Privacy Policy Checkbox @ Checkout
    Here’s a snippet regarding the checkout page. If you’ve been affected by GDPR, you will know you now need users to give you Privacy Policy consent. Or, you might need customer to acknowledge special shipping requirements for example. So, how do we display an additional tick box on the Checkout page (together with the existing […]
  • WooCommerce: Redirect to Custom Thank you Page
    How can you redirect customers to a beautifully looking, custom, thank you page? Thankfully you can add some PHP code to your functions.php or install a simple plugin and define a redirect to a custom WordPress page (as opposed to the default order-received endpoint). This is a great way for you to add specific up-sells, […]
  • WooCommerce: Disable Payment Gateway by Country
    You might want to disable PayPal for non-local customers or enable a specific gateway for only one country… Either way, this is a very common requirement for all of those who trade internationally. Here’s a simple snippet you can further customize to achieve your objective. Simply pick the payment gateway “slug” you want to disable/enable […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

29 thoughts on “WooCommerce: Upload File @ Checkout Page

  1. Hey,

    This code worked great! Thank you. How would I add 2 upload fields rather than just the one?

    Thanks,
    Javi

    1. 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!

  2. I need to customize this for multiple files input file. Any guides?

    1. 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!

  3. 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????

    1. 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

  4. How are you Rodolfo Melogli

    How can I make it not to be required*?
    PHP Snippet: Allow Customers to Upload Files @ WooCommerce Checkout Page

    1. Sure, you can remove the function bbloomer_validate_new_checkout_field and the hook that triggers that function.

  5. 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.

    1. 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?

  6. 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

    1. This should show a link to the image, and not an image. Screenshot please?

  7. Hi!

    Appreciate the help so much! How do we limit the size of the upload?

    Much Thanks,
    IL

    1. 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!

  8. 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?

    add_filter( 'woocommerce_checkout_fields', 'customs_override_checkout_field' );
    function customs_override_checkout_field( $fields ) {
        if( current_user_can('subscriber') ){
    	unset( $fields[ 'billing' ][ 'billing_workemail' ] );
    	unset( $fields['appform']['appform_field'] );//How to hide a field from non-logged in users?
        }
        return $fields;
    }
    
    1. 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!

  9. 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.

    1. 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!

      1. Thank you Rodolfo. I understand.

  10. 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 ”

    add_action( 'woocommerce_checkout_before_customer_details', 'webroom_check_if_product_in_cart' );
    function webroom_check_if_product_in_cart() {
       $product_id = 2299;
       $product_cart_id = WC()->cart->generate_cart_id( $product_id );
       $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
       if ( $in_cart )
        {
        	add_action( 'woocommerce_after_order_notes', 'bbloomer_checkout_file_upload' );
        	}
    .........(all code)
    

    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

    1. 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!

  11. Still works

  12. Great, but just dont know how to remove that it is not required still?

    1. Simply remove this:

      add_action( 'woocommerce_checkout_process', 'bbloomer_validate_new_checkout_field' );
      1. Great Awesome!! Thanks have a nice weekend!

  13. No questions – just praise. Thank you for sharing an elegant solution.

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *