minemindmedia
minemindmedia

Reputation: 407

Get WooCommerce product SKU in product pages meta box

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.

Upvotes: 2

Views: 11732

Answers (4)

silwalprabin
silwalprabin

Reputation: 647

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);

Upvotes: 0

minemindmedia
minemindmedia

Reputation: 407

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.

Upvotes: 5

LoicTheAztec
LoicTheAztec

Reputation: 254448

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:

Upvotes: 6

mlimon
mlimon

Reputation: 671

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

like:

global $product;
echo 'SKU: ' . $product->get_sku();

Upvotes: 0

Related Questions