bmex63

WooCommerce 2.0 Custom Product Tab

Mar 25th, 2013
3,972
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  * Custom Tabs for Product Display. Compatible with WooCommerce 2.0+ only!
  5.  *
  6.  * Outputs an extra tab to the default set of info tabs on the single product page.
  7.  * This file needs to be called via your functions.php file.
  8.  */
  9.  
  10. function custom_tab_options_tab() {
  11. ?>
  12.     <li class="custom_tab"><a href="#custom_tab_data"><?php _e('Custom Tab', 'woothemes'); ?></a></li>
  13. <?php
  14. }
  15. add_action('woocommerce_product_write_panel_tabs', 'custom_tab_options_tab');
  16.  
  17.  
  18. /**
  19.  * Custom Tab Options
  20.  *
  21.  * Provides the input fields and add/remove buttons for custom tabs on the single product page.
  22.  */
  23. function custom_tab_options() {
  24.     global $post;
  25.    
  26.     $custom_tab_options = array(
  27.         'title' => get_post_meta($post->ID, 'custom_tab_title', true),
  28.         'content' => get_post_meta($post->ID, 'custom_tab_content', true),
  29.     );
  30.    
  31. ?>
  32.     <div id="custom_tab_data" class="panel woocommerce_options_panel">
  33.         <div class="options_group">
  34.             <p class="form-field">
  35.                 <?php woocommerce_wp_checkbox( array( 'id' => 'custom_tab_enabled', 'label' => __('Enable Custom Tab?', 'woothemes'), 'description' => __('Enable this option to enable the custom tab on the frontend.', 'woothemes') ) ); ?>
  36.             </p>
  37.         </div>
  38.        
  39.         <div class="options_group custom_tab_options">                                             
  40.             <p class="form-field">
  41.                 <label><?php _e('Custom Tab Title:', 'woothemes'); ?></label>
  42.                 <input type="text" size="5" name="custom_tab_title" value="<?php echo @$custom_tab_options['title']; ?>" placeholder="<?php _e('Enter your custom tab title', 'woothemes'); ?>" />
  43.             </p>
  44.            
  45.             <p class="form-field">
  46.                 <?php _e('Custom Tab Content:', 'woothemes'); ?>
  47.             </p>
  48.            
  49.             <table class="form-table">
  50.                 <tr>
  51.                     <td>
  52.                         <textarea class="theEditor" rows="10" cols="40" name="custom_tab_content" placeholder="<?php _e('Enter your custom tab content', 'woothemes'); ?>"><?php echo @$custom_tab_options['content']; ?></textarea>
  53.                     </td>
  54.                 </tr>  
  55.             </table>
  56.         </div> 
  57.     </div>
  58. <?php
  59. }
  60. add_action('woocommerce_product_write_panels', 'custom_tab_options');
  61.  
  62.  
  63. /**
  64.  * Process meta
  65.  *
  66.  * Processes the custom tab options when a post is saved
  67.  */
  68. function process_product_meta_custom_tab( $post_id ) {
  69.     update_post_meta( $post_id, 'custom_tab_enabled', ( isset($_POST['custom_tab_enabled']) && $_POST['custom_tab_enabled'] ) ? 'yes' : 'no' );
  70.     update_post_meta( $post_id, 'custom_tab_title', $_POST['custom_tab_title']);
  71.     update_post_meta( $post_id, 'custom_tab_content', $_POST['custom_tab_content']);
  72. }
  73. add_action('woocommerce_process_product_meta', 'process_product_meta_custom_tab', 10, 2);
  74.  
  75.  
  76. /**
  77.  * Display Tab
  78.  *
  79.  * Display Custom Tab on Frontend of Website for WooCommerce 2.0
  80.  */
  81.  
  82. add_filter( 'woocommerce_product_tabs', 'woocommerce_product_custom_tab' );
  83.  
  84.  
  85.     function woocommerce_product_custom_tab( $tabs ) {
  86.         global $post, $product;
  87.  
  88.         $custom_tab_options = array(
  89.             'enabled' => get_post_meta($post->ID, 'custom_tab_enabled', true),
  90.             'title' => get_post_meta($post->ID, 'custom_tab_title', true),
  91.             'content' => get_post_meta($post->ID, 'custom_tab_content', true),         
  92.         );
  93.        
  94.             if ( $custom_tab_options['enabled'] != 'no' ){
  95.                 $tabs['custom-tab-first'] = array(
  96.                     'title'    => $custom_tab_options['title'],
  97.                     'priority' => 25,
  98.                     'callback' => 'custom_product_tabs_panel_content',                 
  99.                     'content'  => $custom_tab_options['content']
  100.                 );
  101.             }
  102.  
  103.         return $tabs;
  104.     }
  105.  
  106.     /**
  107.      * Render the custom product tab panel content for the callback 'custom_product_tabs_panel_content'
  108.      */
  109.    function custom_product_tabs_panel_content( $key, $custom_tab_options ) {
  110.         echo '<h2>' . $custom_tab_options['title'] . '</h2>';
  111.         echo $custom_tab_options['content'];
  112.  
  113.     }
  114.  
  115.  
  116. ?>
RAW Paste Data