Last active
July 16, 2021 10:39
-
-
Save jan-koch/270314dfc702ba12e73aa8666f2cfa22 to your computer and use it in GitHub Desktop.
Add a custom SKU field to WooCommerce products.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 add it to the theme's function file or create a separate plugin.
Thanks! Will it have the same behavior as the WP SKU field, including variable products?
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
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