So i want to add 4 fields to a woocommerce checkout page, a subject a title and I want the customer to be able to upload 2 files 1 is a Script and the other is a Intro.
But now I got the problem that the upload files aren't showing on my page. the title and subject are showing but when I set the type to be file it fails.
Here is my code:
// Add custom fields to checkout page
add_filter( 'woocommerce_checkout_fields', 'add_custom_checkout_fields' );
function add_custom_checkout_fields( $fields ) {
$fields['billing']['video_title'] = array(
'type' => 'text',
'class' => array( 'form-row-wide' ),
'label' => __( 'Video Title' ),
'placeholder' => __( 'Enter the title for your video' ),
'required' => true,
'priority' => 1,
);
$fields['billing']['video_subject'] = array(
'type' => 'text',
'class' => array( 'form-row-wide' ),
'label' => __( 'Video Subject' ),
'placeholder' => __( 'Enter the subject of your video' ),
'required' => true,
'priority' => 2,
);
$fields['billing']['video_script'] = array(
'label' => __( 'Video Intro' ),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'priority' => 3,
);
$fields['billing']['video_intro'] = array(
'type' => 'file',
'class' => array( 'form-row-wide' ),
'label' => __( 'Video Intro' ),
'required' => true,
'priority' => 4,
);
return $fields;
}
// Validate custom field values on the checkout page
add_action( 'woocommerce_checkout_process', 'validate_video_fields_on_checkout' );
function validate_video_fields_on_checkout() {
if ( ! isset( $_FILES['video_script']['name'] ) || empty( $_FILES['video_script']['name'] ) ) {
wc_add_notice( __( 'Please upload your video script.' ), 'error' );
}
if ( ! isset( $_FILES['video_intro']['name'] ) || empty( $_FILES['video_intro']['name'] ) ) {
wc_add_notice( __( 'Please upload your video intro.' ), 'error' );
}
}
// Save custom field values as order meta data
add_action( 'woocommerce_checkout_create_order', 'save_video_fields_to_order_meta' );
function save_video_fields_to_order_meta( $order ) {
if ( isset( $_POST['video_subject'] ) ) {
$order->update_meta_data( 'Video Subject', sanitize_text_field( $_POST['video_subject'] ) );
}
if ( isset( $_POST['video_title'] ) ) {
$order->update_meta_data( 'Video Title', sanitize_text_field( $_POST['video_title'] ) );
}
if ( isset( $_FILES['video_script'] ) ) {
$file = $_FILES['video_script'];
$upload = wp_upload_bits( $file['name'], null, file_get_contents( $file['tmp_name'] ) );
if ( isset( $upload['error'] ) && $upload['error'] !== false ) {
wc_add_notice( __( 'There was an error uploading your video script.' ), 'error' );
} else {
$order->update_meta_data( 'Video Script', $upload['url'] );
}
}
if ( isset( $_FILES['video_intro'] ) ) {
$file = $_FILES['video_intro'];
$upload = wp_upload_bits( $file['name'], null, file_get_contents( $file['tmp_name'] ) );
if ( isset( $upload['error'] ) && $upload['error'] !== false ) {
wc_add_notice( __( 'There was an error uploading your video intro.' ), 'error' );
} else {
$order->update_meta_data( 'Video Intro', $upload['url'] );
}
}
}