Wordpress WooCommerce - 在电子邮件中添加订单详细信息

I am trying to customize the "Order Complete" email. Specifically, I have some products (software) that require license keys. So far, I've finished my algorithm for generating and storing the license keys (as well as verification) in a database.

In my child theme, under /woocommerce/order/order-details.php, I've added the corresponding code to add the license keys to the database. I don't want to display them on the order-details page, because the user could close this webpage and then lose the keys. So, I want to send them in an email (or display the keys in both the email and the order-details page). I want to use the "Order-Completed" email, because I don't want to send multiple emails.

I figured out how to add on to the order-details.php template because there was the pre-existing WC_ORDER instance: $order = new WC_Order( $order_id ). After that, I just looked through the documentation to find the correct methods that I needed, and I came up with this:

foreach( $order->get_items() as $item ) {
                $_product     = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
                $item_meta    = new WC_Order_Item_Meta( $item['item_meta'] );

                ?>
                // some code that was already included

                            if ( $_product && $_product->exists() && $_product->is_downloadable() && $order->is_download_permitted() ) {
                                if ($_product->get_attribute('needs_license_key') == "True") { // custom attribute for my software
                                    // generate and store license keys

I only had to add in the if ($_product->get_attribute('needs_license_key')... part, so it was not very complex.

However, this is not present in the Order-Completed template, so I'm not sure where this goes. I'd need to iterate through all the products, check if they are downloadable (I have the code for that already), and then locate the product keys (I know how to do that as well). My main question is: How do I find out what order number the email is being sent for, as well as other stats for that order (such as products listed and their attributes, so I can iterate over them as seen above)? I'm not sure how to do this exactly with WooCommerce.