Get WooCommerce product SKU in product pages meta box
You could try to insert a new metabox SKU field on general options product pages this way:
add_action( 'woocommerce_product_options_general_product_data', 'add_the_sku_to_general_product_field' );function add_the_sku_to_general_product_field() { global $post; $product_sku = get_post_meta( $post->ID, '_sku', true ); echo '<div class="options_group">'; woocommerce_wp_text_input( array( 'id' => '_sku', 'label' => __( 'SKU', 'woocommerce' ), 'placeholder' => '', 'description' => __( 'Enter the SKU', 'woocommerce' ) ) ); echo '</div>';}// Saving the Custom Admin Field in general tab products pages when submittedadd_action( 'woocommerce_process_product_meta', 'save_the_sku_to_general_product_field' );function save_the_sku_to_general_product_field( $post_id ){ $wc_field = $_POST['_sku']; if( !empty($wc_field)) update_post_meta( $post_id, '_sku', esc_attr( $wc_field ) );}
Or alternatively just displaying the SKU…
With this code:
add_action( 'woocommerce_product_options_general_product_data', 'add_the_sku_to_general_product_field' );function add_the_sku_to_general_product_field() { global $post; $product_sku = get_post_meta( $post->ID, '_sku', true ); echo '<div class="options_group">'; echo '<p class="form-field _sku_product "><label for="_sku_product">SKU: </label><span style="font-size:120%;">'.$product_sku.'</span></p>'; echo '</div>';}
As I don't use your plugin, I doesn't guaranty that this should work, but you should try it.
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
References:
Thank you.
The following part of code you suggested it working inside of the plugin now.
global $post;$product_sku = get_post_meta( $post->ID, '_sku', true );
The when I echo $product_sku
it returns the value.
Did you set $product
variable as a global variable ( global $product;
) ? that already @helgatheviking mention it.
like:
global $product;echo 'SKU: ' . $product->get_sku();