I’m currently in the process of creating a file when woocommerce order is completed. The code below may help you.
/**
* Auto Complete all WooCommerce orders then create or append to CSV file on server
*/
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$order->update_status( 'completed' );
$user_info = get_userdata($myuser_id);
$items = $order->get_items();
foreach ($items as $item) {
if ($item['product_id']==92) {
$fileLocation = getenv("DOCUMENT_ROOT") . "/registers/myfile.csv";
$file = fopen($fileLocation,"a");
$content = "\nYOUR TEXT HERE";
fwrite($file,$content);
fclose($file);
/**
* FOR TESTING
*/
$temp = tmpfile();
fwrite($temp, $fileLocation);
fseek($temp, 0);
echo fread($temp, 1024);
}
}
}