Skip to content

Instantly share code, notes, and snippets.

@naurejalam
Last active February 17, 2021 10:54
Show Gist options
  • Save naurejalam/f586c2acbc8a4ca3183dd462b96d71b7 to your computer and use it in GitHub Desktop.
Save naurejalam/f586c2acbc8a4ca3183dd462b96d71b7 to your computer and use it in GitHub Desktop.
Custom SKU Field is added but not showing on Product page (WooCommerce)
Dear All,
I am trying to add a Custome field just near by to SKU and Availablity in Single Product Page in WooCommerce by following code added in my Child Theme function.php.
//****Adding a text field:****//
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 );
}
//**Saving the Custom Meta Field***//
function jk_save_custom_sku( $post_id ) {
// grab the custom SKU from $_POST
$custom_sku = isset( $_POST[ 'jk_sku' ] ) ? sanitize_text_field( $_POST[ 'jk_sku' ] ) : '';
// grab the product
$product = wc_get_product( $post_id );
// save the custom SKU using WooCommerce built-in functions
$product->update_meta_data( 'jk_sku', $custom_sku );
$product->save();
}
add_action( 'woocommerce_process_product_meta', 'jk_save_custom_sku' );
It is added a new Custom SKU Filed in Inventry section successfully. Now I am trying to display it frontend at the product page but it is not showing. Now is the solution for this?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment