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).
Form for variable products
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 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.