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
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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>';
}
Was this article helpful?
YesNo

Where to add custom code?

You should place PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my guide "Should I Add Custom Code Via WP Editor, FTP or Code Snippets?" and my video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything went as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting.

If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

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.

13 thoughts on “WooCommerce: Upload File @ Checkout Page

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

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

  3. Still works

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

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

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. 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 BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

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