Get and display the selected variation SKU in WooCommerce
问题 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'); function wc_product_sku_div() { global $product; return sprintf( '<div class="widget" sp-sku="%s"></div>', $product->get_sku() ); } How can I make it work for both simple and variable products? 回答1: You need the Variation ID to get the SKU of Variable Product. If you pass the Variation ID in the below Function, then you can get its SKU.