order number as file name wordpress/woocommerce

I’m using wordpress with woocommerce and I’ve installed a plugin for file uploads, which put into checkout page, when the order was placed. I need this uploader to change the name of uploaded file to the order number current placed. see the picture…

Order Number as file name by upload

Read More

enter image description here

This put in woocommerce order number in the front-end site

<?php echo $order->get_order_number(); ?>

In functions.php I have this code as test to handle with file name by uploading:

add_filter('wfu_before_file_upload', 'wfu_before_file_upload_handler', 10, 2);

function wfu_before_file_upload_handler($file_path, $file_unique_id) { 
        // separate file name from file path
        $only_filename = basename($file_path);
        $only_filepath = substr($file_path, 0, - strlen($only_filename));
        // separate file extension from file name
        $dot_pos = strrpos($only_filename, ".");
        if ( $dot_pos ) {
                $ext_part = substr($only_filename, $dot_pos);
                $name_part = substr($only_filename, 0, $dot_pos);
        }
        // add timestamp to the end of name_part
        $current_datetime = gmdate("U");
        $only_filename = $name_part . "-" . gmdate("YmdHis", $current_datetime) . $ext_part;
        // return the full file path
        $file_path = $only_filepath . $only_filename;
        return $file_path;
}

This is actually from the plugin developer as example, how to handle file name of uploaded file with wordpress filter.

I would like to make $current_order_number instead of $current_datetime but how I can then get the current order number from database? I’ve tried following:

$order = get_order_number();
$current_order_number = $order;
$new_filename = $current_order_number . $ext_part;
// return the full file path
$file_path = $only_filepath . $new_filename;
return $file_path;

But it’s not working. I still get error message by uploading the file. Any ideas what I’m doing wrong?

Thank you.

Tomas

Related posts