Advertisement
secumbu

Woocommerce Tab with Metabox Values Table

Feb 7th, 2022 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.37 KB | None | 0 0
  1. <?php
  2. // 1) Install Metabox Plugin Free Version
  3. // 2) Create Metaboxes with online generator https://metabox.io/online-generator/ or edit this snippet to your needs
  4.  
  5. add_filter( 'rwmb_meta_boxes', 'woonutrition_register_meta_boxes' );
  6.  
  7. function woonutrition_register_meta_boxes( $meta_boxes ) {
  8.     $prefix = '';
  9.  
  10.     $meta_boxes[] = [
  11.         'title'      => esc_html__( 'Nutrition Info', 'woonutrition' ),
  12.         'id'         => 'nutrition_info',
  13.         'post_types' => ['product'],
  14.         'context'    => 'side',
  15.         'fields'     => [
  16.             [
  17.                 'type' => 'text',
  18.                 'name' => esc_html__( 'Nutrition Value Demo1', 'woonutrition' ),
  19.                 'id'   => $prefix . 'nutrition_value_demo1',
  20.                 'desc' => esc_html__( 'This field is just for demo purposes', 'woonutrition' ),
  21.             ],
  22.             [
  23.                 'type' => 'text',
  24.                 'name' => esc_html__( 'Nutrition Value Demo2', 'woonutrition' ),
  25.                 'id'   => $prefix . 'nutrition_value_demo2',
  26.                 'desc' => esc_html__( 'This field is just for demo purposes', 'woonutrition' ),
  27.             ],
  28.             [
  29.                 'type' => 'text',
  30.                 'name' => esc_html__( 'Nutrition Value Demo3', 'woonutrition' ),
  31.                 'id'   => $prefix . 'nutrition_value_demo3',
  32.                 'desc' => esc_html__( 'This field is just for demo purposes', 'woonutrition' ),
  33.             ],
  34.         ],
  35.     ];
  36.  
  37.     return $meta_boxes;
  38. }
  39.  
  40.  
  41. // 3) Add woocommerce Nutrition Tab
  42.  
  43. add_filter( 'woocommerce_product_tabs', 'woo_nutrition_tab' );
  44.  
  45. function woo_nutrition_tab( $tabs ) {
  46.  
  47.     // Adds the new tab
  48.     $tabs['nutrition_tab'] = array(
  49.         'title'     => __( 'Nutrition Tab', 'woonutrition' ),
  50.         'priority'  => 50,
  51.         'callback'  => 'woo_nutrition_tab_content'
  52.     );
  53.    
  54.     return $tabs;
  55. }
  56. // 4) Add woocommerce Nutrition Tab Content
  57. function woo_nutrition_tab_content() {
  58.    
  59.     // Nutrition tab content
  60.     echo '<h2>' . __( 'Nutrition Tab', 'woonutrition' ) . '</h2>';
  61.     // Create markup for table begin
  62.     echo '<table class="woocommerce-product-attributes shop_attributes">
  63.    <tbody>';
  64.    
  65.     // Check if field has value and show it in table row
  66.     if(rwmb_meta('nutrition_value_demo1')) {
  67.         echo '<tr class="woocommerce-product-attributes-item woocommerce_nutrition_item">
  68.                 <th class="woocommerce-product-attributes-item__label">nutrition_value_demo1</th>
  69.                 <td class="woocommerce-product-attributes-item__value">'. rwmb_meta('nutrition_value_demo1') .'</td>
  70.               </tr>';
  71.     };
  72.     if(rwmb_meta('nutrition_value_demo2')) {
  73.         echo '<tr class="woocommerce-product-attributes-item woocommerce_nutrition_item">
  74.                 <th class="woocommerce-product-attributes-item__label">nutrition_value_demo2</th>
  75.                 <td class="woocommerce-product-attributes-item__value">'. rwmb_meta('nutrition_value_demo2') .'</td>
  76.               </tr>';
  77.     };
  78.     if(rwmb_meta('nutrition_value_demo3')) {
  79.         echo '<tr class="woocommerce-product-attributes-item woocommerce_nutrition_item">
  80.                 <th class="woocommerce-product-attributes-item__label">nutrition_value_demo3</th>
  81.                 <td class="woocommerce-product-attributes-item__value">'. rwmb_meta('nutrition_value_demo3') .'</td>
  82.               </tr>';
  83.     };
  84.     // Create markup for table end
  85.     echo '</tbody>
  86. </table>';
  87. }
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement