0

I'm trying to add a custom upload field to my Woocommerce checkout. File shows up no issues. I can check the $_FILES dump and all that when I disable woocommerce ajax. When it's enabled though, $_FILES returns nothing. Is there something I'm missing with this?

add_action('woocommerce_checkout_before_terms_and_conditions', 'checkout_upload_field');
function checkout_upload_field($checkout){
    echo '<div id="po_number_upload_field"><h3>' . __('P.O. Number Upload') . '</h3>';
    echo '<input type="file" name="po_upload" id="po_upload" class="po_upload form-row-wide" />';
    echo '</div>';
}

add_action('woocommerce_checkout_process', 'validate_checkout_upload_fields');
function validate_checkout_upload_fields() {
    if ($_FILES['po_upload']['error'] === 0) {
       //check file size limit < 1mb
       if($_FILES['po_upload']['size'] > 500000){
         wc_add_notice( __( 'File size too big.' ), 'error' );
       }

       //check type
       if($_FILES['po_upload']['type'] != 'application/pdf'){
         wc_add_notice( __( 'File type not allowed.' ), 'error' );
       }
    }

}
paper_robots
  • 251
  • 2
  • 15
  • See http://stackoverflow.com/questions/2320069/jquery-ajax-file-upload how to handle file upload with ajax. Regular `$.post` doesn't handle files. – Alex Blex Oct 24 '16 at 20:11
  • So I need something like: `var formData = new FormData(); formData.append('file', $('#file')[0].files[0]);` But can I hook that into the woocommerce checkout ajax? – paper_robots Oct 24 '16 at 20:32

0 Answers0