I have this code that works for simple product type but not for variable products in WooCommerce:
add_shortcode( \'product_sku_div\', \'wc_product_sku_div\'
To make it work also for variable products and their variations, it requires Javascript (jQuery) to get the selected variation SKU for variable products.
Try the following that works for simple an variable product types, displaying the selected variation SKU for variable products:
add_shortcode( 'product_sku_div', 'wc_product_sku_div');
function wc_product_sku_div() {
global $product;
if( ! is_a('WC_Product', $product) ) {
$product = wc_get_product( get_the_id() );
}
## 1 - For variable products (and their variations)
if( $product->is_type('variable') ) {
ob_start(); // Starting buffering
?>
Code goes in functions.php file of your active child theme (or active theme). Tested and work.