Need to display a custom fields in the related products on a single product page.
I would like to add a meta box to the Add New Product
fields and display results on custom tab on single product page under reviews.I tried using code but nothing shows on the page. Adding extra product tab helps me in adding extra information. So adding, saving and displaying product is what I am looking for.
add_filter( 'add_meta_boxes', 'bhww_core_cpt_metaboxes' );
function bhww_core_cpt_metaboxes( $meta_boxes ) {
//global $prefix;
$prefix = '_bhww_'; // Prefix for all fields
// Add metaboxes to the 'Product' CPT
$meta_boxes[] = array(
'id' => 'bhww_woo_tabs_metabox',
'title' => 'Additional Product Information - <strong>Optional</strong>',
'pages' => array( 'product' ), // Which post type to associate with?
'context' => 'normal',
'priority' => 'default',
'show_names' => true,
'fields' => array(
array(
'name' => __( 'Ingredients', 'cmb' ),
'desc' => __( 'Anything you enter here will be displayed on the Ingredients tab.', 'cmb' ),
'id' => $prefix . 'ingredients_wysiwyg',
'type' => 'wysiwyg',
'options' => array( 'textarea_rows' => 5, ),
),
array(
'name' => __( 'Benefits', 'cmb' ),
'desc' => __( 'Anything you enter here will be displayed on the Benefits tab.', 'cmb' ),
'id' => $prefix . 'benefits_wysiwyg',
'type' => 'wysiwyg',
'options' => array( 'textarea_rows' => 5, ),
),
),
);
return $meta_boxes;
}
/////////////////////////////////////////
add_filter( 'woocommerce_product_data_tabs', 'bhww_woo_extra_tabs' );
function bhww_woo_extra_tabs( $tabs1 ) {
global $post;
$product_ingredients = get_post_meta( $post->ID, '_bhww_ingredients_wysiwyg', true );
$product_benefits = get_post_meta( $post->ID, '_bhww_benefits_wysiwyg', true );
if ( ! empty( $product_ingredients ) ) {
$tabs1['ingredients_tab'] = array(
'title' => __( 'Ingredients', 'woocommerce' ),
'priority' => 15,
'callback' => 'bhww_woo_ingredients_tab_content'
);
}
if ( ! empty( $product_benefits ) ) {
$tabs1['benefits_tab'] = array(
'title' => __( 'Benefits', 'woocommerce' ),
'priority' => 16,
'callback' => 'bhww_woo_benefits_tab_content'
);
}
return $tabs1;
}
function bhww_woo_ingredients_tab_content() {
global $post;
$product_ingredients = get_post_meta( $post->ID, '_bhww_ingredients_wysiwyg', true );
if ( ! empty( $product_ingredients ) ) {
echo '<h2>' . esc_html__( 'Product Ingredients', 'woocommerce' ) . '</h2>';
// Updated to apply the_content filter to WYSIWYG content
echo apply_filters( 'the_content', $product_ingredients );
}
}
function bhww_woo_benefits_tab_content() {
global $post;
$product_benefits = get_post_meta( $post->ID, '_bhww_benefits_wysiwyg', true );
if ( ! empty( $product_benefits ) ) {
echo '<h2>' . esc_html__( 'Product Benefits', 'woocommerce' ) . '</h2>';
// Updated to apply the_content filter to WYSIWYG content
echo apply_filters( 'the_content', $product_benefits );
}
}