Say you have a wordpress + woocommerce site where some of your products have a price and some don’t, simply because they are custom-made products and you need the customer to first contact you before you get an order or for whatever other reason you have no price but still want to get inquiries about a product.
Unfortunately, even though I’ve searched high and low for a plugin that could add an inquiry form to products with no price I couldn’t find any. Most plugins out there display an inquiry form to ALL of your products even in those that have a price hence an “Add To Cart” button which is not what I wanted. So I had to figure out a way to code it myself. It turned out that I lost much more time searching for a plugin to do the job than actually coding the solution myself. Isn’t that the case most of the time?
The solution requires you to override a single template file from the Woocommerce plugin directory in your theme. In case your theme already contains the overridden file then you will need to create a child theme to put your custom template file. I strongly encourage you to create a child theme when you want to make changes to your theme’s code otherwise you will lose your changes every time you update your theme with a new version.
So log in to your site with FTP and copy the following file
/wp-content/plugins/woocommerce/templates/single-product/add-to-cart/simple.php
inside your child theme
/wp-content/themes/your-child-theme/woocommerce/single-product/add-to-cart/simple.php
Now edit the simple.php file and comment out line 14 where it says
if ( ! $product->is_purchasable() ) return;
What that line actually does is returns with doing nothing if the product is not purchasable, that is, have a price. But we don’t want that, right? What we want is to display a form or put some code exactly when a product has no price. So we comment out that line (you can change it directly if you want but I always leave the original so that I remember what I actually changed) and write:
if ( ! $product->is_purchasable() ) {
// write your code here
return;
}
In my case, I decided to display a Contact Form 7 form, you can display any form from a form plugin provided it gives you a shortcode or a php code to use. You could even write code to build your own form. I wanted my form to display when the user clicked on a link or a button-like link since I decided that a form on the page would be better than a pop-up form could potentially not display great in mobile phone users. I wanted my form to remain hidden until the user decided to click on the link so that it didn’t get too much of the product’s page space.
So I used a little javascript and some php
if ( ! $product->is_purchasable() ) { ?> <script type="text/javascript" language="JavaScript"><!-- function ShowHide(d) { if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; } else { document.getElementById(d).style.display = "none"; } } //--></script> <a href="javascript:ShowHide('product-inquiry-form')"> <h2 class="button">Product Inquiry Form</h2> </a> <div id="product-inquiry-form" style="display:none;"> <div style="clear:both;"> <?php echo do_shortcode( 'Error: Contact form not found.
' ); ?> </div> </div> <?php return; }
The Error: Contact form not found.<?php echo do_shortcode( '
line is the shortcode that will display your form.
So, there you have it: a simple way to display an inquiry form for woocommerce products with no price. No plugins, no updates needed, just a woocommerce template file override.