I'm having a lot of trouble getting the product SKU for a product in a single product page inside of functions.php. I have multiple single product pages and I want different text to display depending on the product. I've created a child theme and I'm working in the functions.php file. I'm new to wordpress and editing themes so I don't quite understand the order of operations yet. I was able to get code working to loop through and give me all the SKU's for all the products but that's independent of the actual page that I'm on.
I've tried a bunch of things. The common solution seems to be:
global $product;
echo $product->get_sku();
but that doesn't work. For some reason the $product variable is null inside the functions.php script.
The loop that I have loops through all the product posts and gets the post ID. I also tried getting the ID of the current page but was also unsuccessful in doing that (the code below was copied from another site). Any help would be greatly appreciated. Thanks.
$full_product_list = array();
$loop = new WP_Query( array( 'post_type' => array('product', 'product_variation'), 'posts_per_page' => -1 ) );
while ( $loop->have_posts() ) : $loop->the_post();
$theid = get_the_ID();
$product = new WC_Product($theid);