I completed the code for a product enquiry form for a variable product using Ninja Forms.
Two weeks ago I wrote some foundation code that would allow for the options in a select field in Ninja Forms to be modified dynamically. At the end of the post I said that I didn’t have the time to complete the code but that it should not be too hard do so. Here is that completed code.
Ninja Forms setup
After activating Ninja Forms I modified the default form to add a hidden field to it. The form will be used for simple products and the hidden field will contain the product title (aka post title).
In the Email Notification I added this field to the email body.
Form for variable products
I then duplicated the form and added a Select dropdown. I removed the default options values and I gave the field key a custom value. This field key will be used in the code.
Now we’re ready to get coding.
Contact form on product pages
I borrowed the code from the Product Enquiry form using WooCommerce and Contact Form 7 post where it uses the ‘the_content’ filter to append the contact form to the product (long) description.
I decided to use the Ninja Forms functions to render the form instead of using do_shortcode().
Retrieving product variations
Products can contain multiple attributes (e.g. size or colour) that are combined to generate multiple variations (e.g. “Small Green”, “Medium Black”, “Large White” etc.). These variations will be presented to the site visitor.
The code goes through the available variations, then the extracts the attributes for each variation and finally creates strings of the variations.
The chosen variation can be added to the email notification.
The specified variation does end up in the email but it doesn’t display in the ‘Submissions‘ area in the Dashboard. I don’t know why this is.
Hey !
Your code works very well ! Have you found why slect value don’t show in Submission section?
@Loïc: No, I never found out why the select value is not displayed in the Submission section. I found the Ninja Forms code hard to follow. I had to do a lot of experimenting to get the code working. If you can figure it out I would love to know.
7 months later and I have found the issue and a solution! https://www.damiencarbery.com/2019/01/ninja-forms-display-dynamic-drop-down-selection-in-submissions/ The submitted value is in the database so I have modified my code to retrieve it and add it to the ‘Select’ dropdown if not already present.
Hello, Can you teach me how can i use this code? Where can i add it? I need to make one enquery form below add to cart button at every product page. I need to have one button that will write “quick order”. After somebody click this button there will be show like accordeon one form with some rows for entering that data. I need before someone click final button “order now” to add Name, telephone, to choose a size for product variables and then click order now button. After that in the mail form i need to received data like link to this product, whitch size the client is choose, the name and telephone. Can i achieve this in the form? This is the example site that the form is working well : https://kartelito.com/mzhko-prehodno-yake-50022414.html
Its in cirillyc letters. Just click button “Купи без регистрация” And you will see the form.,
If you use the ‘Full Code’ (at the bottom of the post) you can upload that file to wp-content/plugins and then activate the plugin. You do not need to modify your functions.php file.
You will have to create a form for simple products and another one for variable products – follow the instructions earlier in the post. You will have to update the ‘Ninja_Forms()->display( 1 )’ line to match the ID number of your forms.
Hi, I can’t get this to work for variable products. Seems to be fine for simple ones.
I’ve copied the full code and setup the forms based on your images..
Any ideas?
Just an update,
It shows on a couple variable products but not others
2nd update,
I found out it only shows if you have a description but doesn’t if you have no text in the box. How can I move this to the short description location instead?
@Sean: I see in the WooCommerce code that the ‘Description’ tab is only added when the product has a long description. When a long description is displayed the ‘the_content’ filter is executed and therefore isn’t run if it’s empty.
You could try something like the following:
add_action( 'woocommerce_single_product_summary', 'dcwd_add_ninja_under_short_description', 25 ); function dcwd_add_ninja_under_short_description() { echo dcwd_add_ninja_to_all_products(); }
I haven’t tried this because I am very busy at the moment.Hi, great code! Small problem though: I tried the code and the list of variations appears but when I try to send the form it keeps on processing and doesn’t send. When I test it without the select dropdown it works and submits. Any thoughts on what might cause that? Thanks in advance!
@Ramon – That sounds like there could be a PHP syntax error somewhere, maybe in the code for the select dropdown. Please alook at the server error log to see if there are any warnings or errors.