Adding a Questions & Answers (Q&A) section to your WooCommerce product pages can improve customer engagement and reduce pre-sale queries. This tutorial will show you how to add a Q&A tab where customers can ask questions, and you can answer them from the admin panel.
We’ll break this down into simple steps with explanations for each part of the code.
Step 1: Add a New Tab to Product Pages
First, we use a filter to add a new tab called “Q&A” on the product page.
add_filter( 'woocommerce_product_tabs', function( $tabs ) {
$tabs['qa_tab'] = [
'title' => 'Q&A',
'priority' => 50,
'callback' => 'render_product_qa_tab',
];
return $tabs;
});
- This hook inserts a new tab titled Q&A on the product detail page.
- The
callback
tells WooCommerce which function to call to display the content of the tab.
Step 2: Display the Q&A Tab Content
Now we create the render_product_qa_tab()
function to display existing questions and a form to submit new ones.
function render_product_qa_tab() {
global $post;
$product_id = $post->ID;
$qa_data = get_post_meta( $product_id, '_product_qa', true ) ?: [];
- We get the current product ID and any saved Q&A data.
Then we show the list of questions and answers:
echo '<div class="product-qa">';
echo '<h3>Questions & Answers</h3>';
if ( $qa_data ) {
foreach ( $qa_data as $entry ) {
echo '<div>';
echo '<p><strong>Q:</strong> ' . esc_html( $entry['question'] ) . '</p>';
if ( ! empty( $entry['answer'] ) ) {
echo '<p><strong>A:</strong> ' . esc_html( $entry['answer'] ) . '</p>';
} else {
echo '<p><em>Awaiting answer from admin...</em></p>';
}
echo '</div>';
}
} else {
echo '<p>No questions have been asked yet.</p>';
}
Then, we add a form so logged-in users can ask questions:
if ( is_user_logged_in() ) {
echo '<form method="post">';
wp_nonce_field( 'submit_product_question', 'qa_nonce' );
echo '<label>Ask a question:</label>';
echo '<textarea name="qa_question" required></textarea>';
echo '<button type="submit" name="submit_qa">Submit Question</button>';
echo '</form>';
} else {
echo '<p>You must <a href="' . esc_url( wp_login_url() ) . '">log in</a> to ask a question.</p>';
}
echo '</div>';
}
Step 3: Handle Question Submission
Next, we handle the form submission using this code:
add_action( 'template_redirect', function() {
if (
isset( $_POST['submit_qa'] ) &&
isset( $_POST['qa_question'], $_POST['qa_nonce'] ) &&
wp_verify_nonce( $_POST['qa_nonce'], 'submit_product_question' )
) {
$product_id = get_the_ID();
$qa_data = get_post_meta( $product_id, '_product_qa', true ) ?: [];
$qa_data[] = [
'question' => sanitize_text_field( $_POST['qa_question'] ),
'answer' => '',
'user' => get_current_user_id(),
'date' => current_time( 'mysql' )
];
update_post_meta( $product_id, '_product_qa', $qa_data );
wp_safe_redirect( get_permalink( $product_id ) . '#tab-qa_tab' );
exit;
}
});
This code saves the question in product meta when submitted and redirects back to the Q&A tab.
Step 4: Add a Q&A Meta Box in Admin Panel
To make it easy to answer questions, we add a new section in the product edit screen in the WordPress admin:
add_action( 'add_meta_boxes', function() {
add_meta_box( 'product_qa_meta', 'Product Q&A', 'render_product_qa_meta_box', 'product', 'normal', 'default' );
});
function render_product_qa_meta_box( $post ) {
$qa_data = get_post_meta( $post->ID, '_product_qa', true ) ?: [];
if ( empty( $qa_data ) ) {
echo '<p>No questions submitted yet.</p>';
return;
}
echo '<table class="widefat striped"><thead><tr><th>Question</th><th>Answer</th></tr></thead><tbody>';
foreach ( $qa_data as $index => $entry ) {
echo '<tr>';
echo '<td>' . esc_html( $entry['question'] ) . '</td>';
echo '<td>';
printf(
'<textarea name="qa_answers[%d]" rows="2">%s</textarea>',
$index,
esc_textarea( $entry['answer'] ?? '' )
);
echo '</td>';
echo '</tr>';
}
echo '</tbody></table>';
}
Step 5: Save Admin Answers
Finally, we save the answers entered by the admin when the product is updated:
add_action( 'save_post_product', function( $post_id ) {
if ( isset( $_POST['qa_answers'] ) ) {
$qa_data = get_post_meta( $post_id, '_product_qa', true ) ?: [];
foreach ( $_POST['qa_answers'] as $index => $answer ) {
if ( isset( $qa_data[ $index ] ) ) {
$qa_data[ $index ]['answer'] = sanitize_text_field( $answer );
}
}
update_post_meta( $post_id, '_product_qa', $qa_data );
}
});
Final Thoughts
This custom solution creates a fully functional Q&A section on WooCommerce product pages:
- Customers can ask questions.
- Admins can answer from the dashboard.
- All questions and answers are visible on the front end.
This is a great way to improve transparency and help customers before they make a purchase decision.