Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // 1) Install Metabox Plugin Free Version
- // 2) Create Metaboxes with online generator https://metabox.io/online-generator/ or edit this snippet to your needs
- add_filter( 'rwmb_meta_boxes', 'woonutrition_register_meta_boxes' );
- function woonutrition_register_meta_boxes( $meta_boxes ) {
- $prefix = '';
- $meta_boxes[] = [
- 'title' => esc_html__( 'Nutrition Info', 'woonutrition' ),
- 'id' => 'nutrition_info',
- 'post_types' => ['product'],
- 'context' => 'side',
- 'fields' => [
- [
- 'type' => 'text',
- 'name' => esc_html__( 'Nutrition Value Demo1', 'woonutrition' ),
- 'id' => $prefix . 'nutrition_value_demo1',
- 'desc' => esc_html__( 'This field is just for demo purposes', 'woonutrition' ),
- ],
- [
- 'type' => 'text',
- 'name' => esc_html__( 'Nutrition Value Demo2', 'woonutrition' ),
- 'id' => $prefix . 'nutrition_value_demo2',
- 'desc' => esc_html__( 'This field is just for demo purposes', 'woonutrition' ),
- ],
- [
- 'type' => 'text',
- 'name' => esc_html__( 'Nutrition Value Demo3', 'woonutrition' ),
- 'id' => $prefix . 'nutrition_value_demo3',
- 'desc' => esc_html__( 'This field is just for demo purposes', 'woonutrition' ),
- ],
- ],
- ];
- return $meta_boxes;
- }
- // 3) Add woocommerce Nutrition Tab
- add_filter( 'woocommerce_product_tabs', 'woo_nutrition_tab' );
- function woo_nutrition_tab( $tabs ) {
- // Adds the new tab
- $tabs['nutrition_tab'] = array(
- 'title' => __( 'Nutrition Tab', 'woonutrition' ),
- 'priority' => 50,
- 'callback' => 'woo_nutrition_tab_content'
- );
- return $tabs;
- }
- // 4) Add woocommerce Nutrition Tab Content
- function woo_nutrition_tab_content() {
- // Nutrition tab content
- echo '<h2>' . __( 'Nutrition Tab', 'woonutrition' ) . '</h2>';
- // Create markup for table begin
- echo '<table class="woocommerce-product-attributes shop_attributes">
- <tbody>';
- // Check if field has value and show it in table row
- if(rwmb_meta('nutrition_value_demo1')) {
- echo '<tr class="woocommerce-product-attributes-item woocommerce_nutrition_item">
- <th class="woocommerce-product-attributes-item__label">nutrition_value_demo1</th>
- <td class="woocommerce-product-attributes-item__value">'. rwmb_meta('nutrition_value_demo1') .'</td>
- </tr>';
- };
- if(rwmb_meta('nutrition_value_demo2')) {
- echo '<tr class="woocommerce-product-attributes-item woocommerce_nutrition_item">
- <th class="woocommerce-product-attributes-item__label">nutrition_value_demo2</th>
- <td class="woocommerce-product-attributes-item__value">'. rwmb_meta('nutrition_value_demo2') .'</td>
- </tr>';
- };
- if(rwmb_meta('nutrition_value_demo3')) {
- echo '<tr class="woocommerce-product-attributes-item woocommerce_nutrition_item">
- <th class="woocommerce-product-attributes-item__label">nutrition_value_demo3</th>
- <td class="woocommerce-product-attributes-item__value">'. rwmb_meta('nutrition_value_demo3') .'</td>
- </tr>';
- };
- // Create markup for table end
- echo '</tbody>
- </table>';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement