Last active
June 8, 2023 20:30
-
-
Save anderly/264754af7f8ddd942dd9 to your computer and use it in GitHub Desktop.
WooCommerce - Add Custom Product Data Tab
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
// First Register the Tab by hooking into the 'woocommerce_product_data_tabs' filter | |
add_filter( 'woocommerce_product_data_tabs', 'add_my_custom_product_data_tab' ); | |
function add_my_custom_product_data_tab( $product_data_tabs ) { | |
$product_data_tabs['my-custom-tab'] = array( | |
'label' => __( 'My Custom Tab', 'my_text_domain' ), | |
'target' => 'my_custom_product_data', | |
); | |
return $product_data_tabs; | |
} | |
// Next provide the corresponding tab content by hooking into the 'woocommerce_product_data_panels' action hook | |
// See https://github.com/woothemes/woocommerce/blob/master/includes/admin/meta-boxes/class-wc-meta-box-product-data.php | |
// for more examples of tab content | |
// See https://github.com/woothemes/woocommerce/blob/master/includes/admin/wc-meta-box-functions.php for other built-in | |
// functions you can call to output text boxes, select boxes, etc. | |
add_action( 'woocommerce_product_data_panels', 'add_my_custom_product_data_fields' ); | |
function add_my_custom_product_data_fields() { | |
global $woocommerce, $post; | |
?> | |
<!-- id below must match target registered in above add_my_custom_product_data_tab function --> | |
<div id="my_custom_product_data" class="panel woocommerce_options_panel"> | |
<?php | |
woocommerce_wp_checkbox( array( | |
'id' => '_my_custom_field', | |
'wrapper_class' => 'show_if_simple', | |
'label' => __( 'My Custom Field Label', 'my_text_domain' ), | |
'description' => __( 'My Custom Field Description', 'my_text_domain' ), | |
'default' => '0', | |
'desc_tip' => false, | |
) ); | |
?> | |
</div> | |
<?php | |
} |
Probably me being an idiot, but just in case anyone else has the same problem as me; by "Id below must match target registered in above add_my_custom_product_data_tab function", this means the 'target' parameter, not the '$product_data_tabs['my-custom-tab']' part. (Thought there was another error like above till I spotted target!)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You've got 'add_my_custom_product_data_fields' in add_action but the function name is 'add_mailchimp_product_data_fields'. Works a treat once that's rectified ;-)