I am using a code that adds a file upload field on the checkout page. How to add file upload to WooCommerce checkout? Many thanks to @LoicTheAztec for this code.
I have created the files checkout_uploads.php, checkout_upload.js and added the code to functions.php a child theme.
add_action( 'woocommerce_after_order_notes', 'add_custom_checkout_field' );
function add_custom_checkout_field($checkout) {
echo '<div class="woocommerce-additional-fields__field-wrapper">';
woocommerce_form_field('certificate', array(
'type' => 'file',
'class' => array('form-row-wide'),
'label' => __('File', 'woocommerce'),
'required' => false,
'max_size' => '2048',
'accept' => '.pdf,.doc,.docx,.rtf,.txt',
), '');
echo '</div>';
}
// Save the uploaded file URL and name
add_action( 'woocommerce_checkout_create_order', 'save_checkout_uploaded_file', 10, 2 );
function save_checkout_uploaded_file( $order, $data ){
if( $checkout_upload = WC()->session->get('checkout_upload') ) {
$order->update_meta_data( '_checkout_upload', $checkout_upload );
}
WC()->session->__unset('checkout_upload');
}
// Display the uploaded file in admin orders
add_action('woocommerce_admin_order_data_after_billing_address', 'display_uploaded_file_in_admin_orders');
function display_uploaded_file_in_admin_orders( $order ){
if( $checkout_upload = $order->get_meta( '_checkout_upload' ) ) {
printf( '<p>%s <br><a href="%s">%s</a></p>',
__("File Uploaded:", 'woocommerce'),
$checkout_upload['file_url'],
$checkout_upload['file_name']
);
}
}
// Display the uploaded file in thankyou page
add_action('woocommerce_order_details_after_order_table', 'display_uploaded_file_in_thankyou');
function display_uploaded_file_in_thankyou ( $order ){
if( $checkout_upload = $order->get_meta( '_checkout_upload' ) ) {
printf( '<p>%s <br><a href="%s">%s</a></p>',
__("File Uploaded:", 'woocommerce'),
$checkout_upload['file_url'],
$checkout_upload['file_name']
);
}
}
// Display the uploaded file in emails
add_action('woocommerce_email_customer_details', 'display_uploaded_file_in_email');
function display_uploaded_file_in_email ( $order ){
if( $checkout_upload = $order->get_meta( '_checkout_upload' ) ) {
printf( '<p>%s <a href="%s">%s</a></p>',
__("File Uploaded:", 'woocommerce'),
$checkout_upload['file_url'],
$checkout_upload['file_name']
);
}
}
Only one file can be uploaded at the moment. How can I upload multiple files at the same time? I will be glad of your help!