3

enter image description here

I am new in wordpress development. I want to do add settings in a product page in a new tab.

How to I add settings

code That I tried to add Tab

 //add product tab link in admin
        add_action( 'woocommerce_product_write_panel_tabs', array($this,'woocommerce_product_write_panel_tabs' ));
        //add product tab content in admin
        add_action('woocommerce_product_write_panels', array($this,'woocommerce_product_write_panels'));

   /**
 * woocommerce_product_write_panel_tabs
 * Used to add a product custom tab to product edit screen
 * @return void
 */
function woocommerce_product_write_panel_tabs(){
    ?>
    <li class="custom_tab">
        <a href="#custom_tab_data_ctabs">
            <?php _e('Customstock Tabs', 'GWP'); ?>
        </a>
    </li>
    <?php
}

/**
 * woocommerce_product_write_panels
 * Used to display a product custom tab content (fields) to product edit screen
 * @return void
 */
function woocommerce_product_write_panels() {
    global $post,$woocommerce;
    $fields = array(
        array(
            'key'   => 'custom_tabs_ids',
            'label' => __( 'Select Custom Tabs', 'GWP' ),
            'desc'  => __( 'Start typing the Custom Tab name, Used for including custom tabs.', 'GWP' )
        ),
        array(
            'key'   => 'exclude_custom_tabs_ids',
            'label' => __( 'Select Global Tabs to exclude', 'GWP' ),
            'desc'  => __( 'Start typing the Custom Tab name. used for excluding global tabs.', 'GWP' )
        ),
        array(
            'key'   => 'id',
            'label' => __( 'Select Global Tabs to eclude', 'GWP' ),
            'desc'  => __( 'Start typing the Custom Tab name. used for excluding global tabs.', 'GWP' )
        )
    );

}

closed as off-topic by Milo, Howdy_McGee, kaiser Apr 6 '16 at 12:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Did you try WooCommerce doc ? – Sumit Apr 5 '16 at 5:37
  • @Dipika: You have used wrong filters and actions check my answer. I have tried my code on my local system. – Mehul Gohil Apr 5 '16 at 5:54
5

I have worked on your issue and found a solution after some Google.

Note: Add the below mentioned code to theme's functions.php or any plugin's file.

Code:

This filter function will add a custom tab to the Products Data metabox

<?php  
add_filter( 'woocommerce_product_data_tabs', 'add_my_custom_product_data_tab' , 99 , 1 );
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;
}

This action will add custom fields to the added custom tabs under Products Data metabox

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
}
?>

Save custom fields data of products tab:

add_action( 'woocommerce_process_product_meta', 'woocommerce_process_product_meta_fields_save' );
function woocommerce_process_product_meta_fields_save( $post_id ){
    // This is the case to save custom field data of checkbox. You have to do it as per your custom fields
    $woo_checkbox = isset( $_POST['_my_custom_field'] ) ? 'yes' : 'no';
    update_post_meta( $post_id, '_my_custom_field', $woo_checkbox );
}

Hope this helps!

  • If I want to save content of custom filed value in database then how i can store? – Dipika Apr 5 '16 at 12:27
  • @Dipika: Please find my updated answer in my earlier answer post. – Mehul Gohil Apr 5 '16 at 12:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.