Advertisement
bmex63

WooCommerce 2.0 Custom Product Tab (uses WP Code Editor)

Mar 25th, 2013
2,747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.59 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Custom Tabs for Custom Specifications Display. Compatible with WooCommerce 2.0+ only!
  5.  * This version uses the code editor.
  6.  *
  7.  * Outputs an extra tab to the default set of info tabs on the single product page.
  8.  * This file needs to be called via your functions.php file.
  9.  */
  10. function custom_tab_options_tab_spec() {
  11. ?>
  12.     <li class="custom_tab3"><a href="#custom_tab_data3"><?php _e('Custom Tab 3', 'woothemes'); ?></a></li>
  13. <?php
  14. }
  15. add_action('woocommerce_product_write_panel_tabs', 'custom_tab_options_tab_spec');
  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_spec() {
  24.     global $post;
  25.    
  26.     $custom_tab_options_spec = array(
  27.         'titlec' => get_post_meta($post->ID, 'custom_tab_title_spec', true),
  28.         'contentc' => get_post_meta($post->ID, 'custom_tab_content_spec', true),
  29.     );
  30.    
  31. ?>
  32.     <div id="custom_tab_data3" 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_spec', '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_spec" value="<?php echo @$custom_tab_options_spec['titlec']; ?>" 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. <?php
  53.         $settings = array(
  54.                         'text_area_name'=> 'custom_tab_content_spec',
  55.                         'quicktags'     => true,
  56.                         'tinymce'       => true,
  57.                         'media_butons'  => false,
  58.                         'textarea_rows' => 98,
  59.                         'editor_class'  => 'contra',
  60.                         'editor_css'    => '<style>#wp-custom_tab_content_spec-editor-container .wp-editor-area{height:250px; width:100%;} #custom_tab_data3 .quicktags-toolbar input {width:auto;}</style>'
  61.                         );
  62.                        
  63.         $id = 'custom_tab_content_spec';
  64.  
  65.  wp_editor($custom_tab_options_spec['contentc'],$id,$settings);
  66.  
  67.  ?>
  68.                     </td>
  69.                 </tr>  
  70.             </table>
  71.         </div> 
  72.     </div>
  73. <?php
  74. }
  75. add_action('woocommerce_product_write_panels', 'custom_tab_options_spec');
  76.  
  77.  
  78. /**
  79.  * Process meta
  80.  *
  81.  * Processes the custom tab options when a post is saved
  82.  */
  83. function process_product_meta_custom_tab_spec( $post_id ) {
  84.     update_post_meta( $post_id, 'custom_tab_enabled_spec', ( isset($_POST['custom_tab_enabled_spec']) && $_POST['custom_tab_enabled_spec'] ) ? 'yes' : 'no' );
  85.     update_post_meta( $post_id, 'custom_tab_title_spec', $_POST['custom_tab_title_spec']);
  86.     update_post_meta( $post_id, 'custom_tab_content_spec', $_POST['custom_tab_content_spec']);
  87. }
  88. add_action('woocommerce_process_product_meta', 'process_product_meta_custom_tab_spec', 10, 2);
  89.  
  90.  
  91. /**
  92.  * Display Tab
  93.  *
  94.  * Display Custom Tab on Frontend of Website for WooCommerce 2.0
  95.  */
  96.  
  97. add_filter( 'woocommerce_product_tabs', 'woocommerce_product_custom_tab_spec' );
  98.  
  99.  
  100.     function woocommerce_product_custom_tab_spec( $tabs ) {
  101.         global $post, $product;
  102.  
  103.         $custom_tab_options_spec = array(
  104.             'enabled' => get_post_meta($post->ID, 'custom_tab_enabled_spec', true),
  105.             'titlec' => get_post_meta($post->ID, 'custom_tab_title_spec', true),
  106.             'contentc' => get_post_meta($post->ID, 'custom_tab_content_spec', true),           
  107.         );
  108.        
  109.             if ( $custom_tab_options_spec['enabled'] != 'no' ){
  110.                 $tabs['custom-tab-second'] = array(
  111.                     'title'    => $custom_tab_options_spec['titlec'],
  112.                     'priority' => 30,
  113.                     'callback' => 'custom_product_tabs_panel_content_spec',                
  114.                     'content'  => $custom_tab_options_spec['contentc']
  115.                 );
  116.             }
  117.         return $tabs;
  118.     }
  119.  
  120.     /**
  121.      * Render the custom product tab panel content for the callback 'custom_product_tabs_panel_content_spec'
  122.      */
  123.          
  124.    function custom_product_tabs_panel_content_spec( $key, $custom_tab_options_spec ) {
  125.  
  126.         global $post, $product;
  127.  
  128.         $custom_tab_options_spec = array(
  129.             'enabled' => get_post_meta($post->ID, 'custom_tab_enabled_spec', true),
  130.             'titlec' => get_post_meta($post->ID, 'custom_tab_title_spec', true),
  131.             'contentc' => get_post_meta($post->ID, 'custom_tab_content_spec', true),           
  132.         );  
  133.    
  134.         echo '<h2>' . $custom_tab_options_spec['titlec'] . '</h2>';
  135.         echo $custom_tab_options_spec['contentc'];
  136.  
  137.     }
  138.  
  139.  
  140. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement