By default your Woocommerce setup shows “Out of stock” message without any “add to cart” button or any other message. In this post I am showing you how to show a contact form only when your product status is “Out of stock”.
This is especially useful for the customers who would like to ask additional questions about the product itself or about whether or when it will be available.
How to Show Contact Form Only When Product is Out Of Stock?
Step 1: create a contact form
You can use whatever WordPress contact form plugin for that but keep in mind that If you would like your contact form entries to show you the product name and URL then you need your form to be able to add hidden fields and embed “post title” or “permalink” data. See the screenshot below.
In this tutorial I am using the Fluent Forms plugin which is a simple to use and offers lots of great features.
Step 2: Copy the code to the Code snippets code box
Code Snippets plugin is a free plugin which allows your to run PHP/HTML/CSS/JavaScript code snippets on your site without the need to modify functions.php. The plugin can be found here. Another option, paste the code to your child theme’s functions.php file.
/* Show contact form instead of "Out Of Stock" message */
add_action('woocommerce_single_product_summary', 'out_of_stock_show_form', 20);
function out_of_stock_show_form() {
global $product;
if(!$product->is_in_stock( )) {
echo '<div class="your-form">';
echo 'Unfortunately this product is out of stock.<br>Contact us for more information';
echo do_shortcode('[your-shortcode]'); // Replace with your own contact form shortcode
echo '</div>';
}
}
/* This will show your contact form when the variation is out of stock */
add_filter( 'woocommerce_available_variation', 'variation_out_of_stock_show_form', 10, 3 );
function variation_out_of_stock_show_form( $data, $product, $variation ) {
if( ! $data['is_in_stock'] )
{
$data['availability_html'] = '<div class="your-form">';
$data['availability_html'] .= 'Unfortunately this product is out of stock.<br>Contact us for more information';
$data['availability_html'] .= do_shortcode('[your-shortcode]'); // Replace with your own contact form shortcode
$data['availability_html'] .= '</div>';
}
return $data;
}
Pay attention the line which contains [your-shortcode]. This is the place you should add your own contact form shortcode.