Skip to content

Instantly share code, notes, and snippets.

@jan-koch
Last active July 16, 2021 10:39
Show Gist options
  • Save jan-koch/270314dfc702ba12e73aa8666f2cfa22 to your computer and use it in GitHub Desktop.
Save jan-koch/270314dfc702ba12e73aa8666f2cfa22 to your computer and use it in GitHub Desktop.
Add a custom SKU field to WooCommerce products.
<?php
function jk_add_custom_sku() {
$args = array(
'label' => __( 'Custom SKU', 'woocommerce' ),
'placeholder' => __( 'Enter custom SKU here', 'woocommerce' ),
'id' => 'jk_sku',
'desc_tip' => true,
'description' => __( 'This SKU is for internal use only.', 'woocommerce' ),
);
woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_sku', 'jk_add_custom_sku' );
function jk_save_custom_meta( $post_id ) {
// grab the SKU value
$sku = isset( $_POST[ 'jk_sku' ] ) ? sanitize_text_field( $_POST[ 'jk_sku' ] ) : '';
// grab the product
$product = wc_get_product( $post_id );
// save the custom SKU meta field
$product->update_meta_data( 'jk_sku', $sku );
$product->save();
}
add_action( 'woocommerce_process_product_meta', 'jk_save_custom_meta' );
@ibd2004
Copy link

ibd2004 commented Jan 16, 2021

Thanks for the code, i was looking for the same! Were do i need to paste this code or upload the file to?

Many thanks!
Itai

@mzdebo
Copy link

mzdebo commented Jan 23, 2021

@ibd2004 add it to the theme's function file or create a separate plugin.

@albertvisuals
Copy link

Thanks! Will it have the same behavior as the WP SKU field, including variable products?

@albertvisuals
Copy link

Have just checked and it sadly not work for variable products, any idea how to make that happen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment