A product enquiry form for a variable product needs a dynamically configurable field - CF7 cannot deliver this but Ninja Forms is flexible.
Early last year I wrote about using Contact Form 7 Dynamic Text Extension to create a product enquiry form for WooCommerce. I recently received a comment asking:
Hmm, how would we use this with a variable product to get the variable SKU?
Can it be done in CF7?
Getting information about the displayed product is easy as the CF7 Dynamic Text Extension plugin can extract the title and ID and other information about the current post (or product in this case). With a variable product we would need to ask the user to specify the variation they are interested in. This requires creating an input element (e.g. radio buttons or ‘select’ dropdown) with an unknown number of elements. This is where we hit the limitations of Contact Form 7.
Trying Ninja Forms
I created a default form and added a Select input to it. I have found the developer documentation to be lacking and it is hard to get clear information about the internals from a developer perspective. I think that the documentation has not been updated for version 3 of the plugin – the last blog entry on the front page is from April 2016.
I tried to create a custom field but, regardless of what action I ran it in, the ninja_forms_register_field() function was not available. But I did find another way…
Filter On Render
I somehow found the ‘ninja_forms_render_options’ filter and via this found a post about pre-populating a select field. This looks very promising.
Then it can be used in the filter code to set the options:
The value is displayed in the email. This this example it is a single letter. It would be better to make it clearer but this is just a proof of concept.
WooCommerce Variations – Another Day’s Work
It should not be difficult to expand the code to get the options from a product’s variations. It’s a busy week for me so I don’t have the time to do this but I will get to it.
It might be better to create two forms, one for simple products (omitting the ‘select’ dropdown) and the other with it. The appropriate form would be appended to the product page. The above filter would only be invoked for variable products.