0

I'm trying to add File Upload for checkout process. Here is my code:

Add input after order notes

add_filter( 'woocommerce_after_order_notes', 'upload_file_checkout' );
function upload_file_checkout() { 
    ?>
    
    <p class="form-row" id="image" data-priority="">
        <label for="image" class="">Image (JPG, PNG, PDF)</label>
        <span class="woocommerce-input-wrapper">
        <input type='file' name='image' accept='image/*,.pdf' multiple='false'>
        </span>
    </p>
    
   <?php
}

Next, update order meta

add_action( 'woocommerce_checkout_update_order_meta', 'checkout_file_update_order_meta' );
function checkout_file_update_order_meta($order_id) {    
    
    if ( isset( $_POST['image'] ) ) {
        
        require_once( ABSPATH . 'wp-admin/includes/image.php' );
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
        require_once( ABSPATH . 'wp-admin/includes/media.php' );
        
        $attachment_id = media_handle_upload( 'image', $order_id );

        update_post_meta( $order_id, '_image', $attachment_id );

    }
    
}

And show the result in admin page

add_action( 'woocommerce_admin_order_data_after_billing_address', 'show_new_checkout_field_order', 10, 1 );
   
function show_new_checkout_field_order( $order ) {    
   $order_id = $order->get_id();
   echo '<p><strong>Image:</strong> ' . get_post_meta( $order_id, '_image', true ) . '</p>';
}

But this is not work. How can I fix it?

This is how I see this meta in DB after place an order

enter image description here

3
  • Can you clarify which part isn't working? Is it NOT uploading the file or is it not adding the post_meta? Or is something else going wrong? Commented Jan 24, 2021 at 16:08
  • I get an error sending the file Commented Jan 24, 2021 at 16:45
  • See if you can locate exactly what the error is - everything seems to be working and it's attempting to add the file but at some point WordPress is objecting and throwing an error. Commented Jan 24, 2021 at 16:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.