项目作者: gulchlab

项目描述 :
Remove default tabs and add new tabs using CMB2 metabox - WooCoomerce
高级语言: PHP
项目地址: git://github.com/gulchlab/Modify-Product-Tab-WooCommerce.git
创建时间: 2017-05-18T08:42:52Z
项目社区:https://github.com/gulchlab/Modify-Product-Tab-WooCommerce

开源协议:

下载


Modify Product Tab - WooCommerce

Remove default tabs and add new tabs using CMB2 metabox - WooCoomerce

Customize Product Tabs with CMB2 Repeater

Create repeater field using cmb2.

  1. // repeatable field for product tab
  2. function register_metabox() {
  3. $prefix = 'repeatable_tab_';
  4. $cmb_tab = new_cmb2_box( array(
  5. 'id' => $prefix . 'metabox',
  6. 'title' => __( 'Product Tab', 'cmb2' ),
  7. 'object_types' => array( 'product' ), // Post type
  8. 'context' => 'normal',
  9. 'priority' => 'high',
  10. 'show_names' => true, // Show field names on the left
  11. ) );
  12. // Repeatable group
  13. $group_field = $cmb_tab->add_field( array(
  14. 'id' => $prefix . 'sections',
  15. 'type' => 'group',
  16. 'options' => array(
  17. 'group_title' => __( 'Tab', 'cmb2' ) . ' {#}', // {#} gets replaced by row number
  18. 'add_button' => __( 'Add another tab', 'cmb2' ),
  19. 'remove_button' => __( 'Remove tab', 'cmb2' ),
  20. 'sortable' => true, // beta
  21. ),
  22. ) );
  23. // Tab Title
  24. $cmb_tab->add_group_field( $group_field, array(
  25. 'name' => 'Tab Title',
  26. 'id' => 'tab_title',
  27. 'type' => 'text',
  28. ) );
  29. // Tab editor
  30. $cmb_tab->add_group_field( $group_field, array(
  31. 'name' => __( 'Content', 'cmb2' ),
  32. 'id' => 'tab_content',
  33. 'type' => 'wysiwyg',
  34. 'options' => array( 'textarea_rows' => 8, ),
  35. ) );
  36. }
  37. add_action( 'cmb2_init', 'register_metabox' );

Paste above code in function.php or use example-functions.php as CMB suggest

Now, Below code will unset default tabs and adds new tab field in single product page template

Paste below code in function.php

  1. /**
  2. * Remove default tab
  3. * Add Custom tab, single product page - repeatable field CMB2
  4. */
  5. add_filter( 'woocommerce_product_tabs', 'tradiestandard_product_tab' );
  6. function tradiestandard_product_tab( $tabs ) {
  7. //unset default tabs
  8. unset( $tabs['description'] ); // Remove the description tab
  9. unset( $tabs['reviews'] ); // Remove the reviews tab
  10. unset( $tabs['additional_information'] ); // Remove the additional information tab
  11. // Adds the new tab
  12. $entries = get_post_meta( get_the_ID(), 'repeatable_tab_sections', true );
  13. $counter = 1;
  14. foreach ( (array) $entries as $key => $entry ) {
  15. $counter++;
  16. $tab_content = $tab_title = '';
  17. if ( isset( $entry['tab_title'] ) ) {
  18. $title = esc_html( $entry['tab_title'] ); // tab title value
  19. }
  20. if ( isset( $entry['tab_content'] ) ) {
  21. $desc = wpautop( $entry['tab_content'] ); // tab description content
  22. }
  23. if($title) {
  24. // tab title
  25. $tabs['tradiestandard_product_tab_'.$counter] = array(
  26. 'title' => __( $title, 'tradiestandard' ),
  27. 'priority' => 50+$counter,
  28. 'tabContent' => $desc,
  29. 'callback' => 'tradiestandard_product_tab_content_'
  30. );
  31. }
  32. }
  33. return $tabs;
  34. }
  35. // function to display tab content
  36. function tradiestandard_product_tab_content_( $tab_key, $tab_info ) {
  37. echo apply_filters( 'tab_content', $tab_info['tabContent'] );
  38. }

That’s All. Thank you.