2

I have a custom meta box in the backend of WooCommerce. Currently I have it setup to display some data from the single product page.

I've used <?php the_title(); ?> to display the title of the product and I've used <?php the_field('myfield'); ?> to display some content from an ACF field.

I really want to be able to display the SKU in this meta box as well, but everything I've tried breaks the page.

I tried adding <?php echo $product->get_sku(); ?> and it breaks the page. I've tried a bunch of other stuff to.

I just need to pass the value of the sku on the inventory tab to a meta box on the same admin page.

Can anyone else with this? Thanks.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
minemindmedia
  • 293
  • 2
  • 5
  • 19

4 Answers4

5

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 submitted
add_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…

enter image description here

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:

Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
5

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_skuit returns the value.

minemindmedia
  • 293
  • 2
  • 5
  • 19
0

Did you set $product variable as a global variable ( global $product; ) ? that already @helgatheviking mention it.

like:

global $product;
echo 'SKU: ' . $product->get_sku();
mlimon
  • 661
  • 8
  • 19
  • My meta box is created through a plugin. I've tried setting it as global within the plugin and that doesn't help. – minemindmedia Oct 13 '16 at 00:48
  • @minemind it's not about metabox , you said you tried to get sku value but it break your page so where you used `get_sku(); ?>` just there put `global $product;` before. Does it make sense ? – mlimon Oct 13 '16 at 00:51
  • Yes, that's what I did and it still breaks the page. – minemindmedia Oct 13 '16 at 02:21
  • This is how the meta box looks before trying to get the sku value: http://imgur.com/a/bZHgt This is how it looks after: http://imgur.com/a/uac0A – minemindmedia Oct 13 '16 at 02:28
0

Add this to your functions.php to display SKU (Product Code) in product page:

add_action( 'woocommerce_single_product_summary', 'dev_designs_show_sku', 5 );
function dev_designs_show_sku(){
    global $product;
    echo 'Product Code: ' . $product->get_sku();
}

enter image description here

enter image description here

If you are getting surprised seeing Product Code instead of SKU; I use below code for replacing SKU by product code (as per customer requirement)

function translate_woocommerce($translation, $text, $domain) {
    if ($domain == 'woocommerce') {
        switch ($text) {
            case 'SKU':
                $translation = 'Product Code';
                break;
            case 'SKU:':
                $translation = 'Product Code:';
                break;
        }
    }
    return $translation;
}

add_filter('gettext', 'translate_woocommerce', 10, 3);
silwalprabin
  • 637
  • 7
  • 20