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