WooCommerce: Create Order From Contact Form Submission

So, I’ve built my own event management system for WooCommerce.

My objective was to embed a form on the event registration page, and then programmatically create a free WooCommerce order for that customer – so I can track the number of attendees (orders) and follow up with email automations (customers).

You can already see the system in action on the How to Contribute to WooCommerce Core” event page: you can clearly see an email input and a “Register Now” button inside the “You’re invited” section. That’s the form – actually a Fluent Forms plugin contact form.

And then there is a simple snippet that hooks into the Fluent Form submission, and conditionally generates a WooCommerce order.

In this post, you’ll learn about a quick way to create a Fluent Forms form on your WordPress site, about the “fluentform/validate_input_item_input_email” hook, and finally about the wc_create_order() WooCommerce function that, of course, let us generate an order automatically. Enjoy!

Step 1: Fluent Form

So, I’ve created a form thanks to Fluent Forms with just 1 field and a submit button. The field is an “Email” field, where users will enter their email address.

Here’s a quick screenshot of the form preview:

Now I can use the handy shortcode or Gutenberg block to embed the form on the event page:

Step 2: Code Objective

Now, I need to “intercept” the posted email address and either return an error if the user is already registered or create the WooCommerce order.

By looking at the detailed Fluent Forms documentation, I found this specific hook: “fluentform/validate_input_item_input_email” (link to relevant Fluent Forms doc). This basically does the trick as it gives me access to the posted email value, and I can return an error message or otherwise execute some code.

What I need to do from a WooCommerce point of view, once I have access to the email, is check if the customer has a previous order (so they’re registered), and if not create the order. Very easy!

Step 3: PHP Snippet to Create a WooCommerce Order Upon a Fluent Form Submission

Requirements:

  • Fluent form ID (8 in my case)
  • WooCommerce hidden, simple, free product ID (240779 in my case) with name = “Event XYZ”
/**
 * @snippet       Create Woo Order From Fluent Forms
 * @tutorial      Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'fluentform/validate_input_item_input_email', 'bbloomer_maybe_register_attendee', 9999, 5 );

function bbloomer_maybe_register_attendee( $errorMessage, $field, $formData, $fields, $form ) {
	if ( $form->id != 8 ) return $errorMessage; // FORM TARGET
	$email = $formData['email']; // GET EMAIL VALUE
	if ( wc_customer_bought_product( $email, '', 240779 ) ) {
		$errorMessage = "You are already registered!";
	} else {
		$order = wc_create_order( [ 'created_via' => 'fluent' ] );
		$order->add_product( get_product( '240779' ), 1 );
		$order->set_address( [ 'email' => $email ], 'billing' );
		$order->calculate_totals();
		$order->update_status( 'completed' );
	}
	return $errorMessage;
}

As you can see, if there is an existing order we return an error message; otherwise we use the wc_create_order WooCommerce function to programmatically generate an order with product ID 240779 and quantity 1, with the correct billing email address and with status completed.

That’s all!

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Product Enquiry Form @ Single Product Page (CF7)
    A client asked to show a “Product Inquiry” button on the Single Product Page which would display, upon click, a Contact Form with an automatically populated subject (Contact Form 7 plugin must be installed of course). Here’s how I did it!
  • WooCommerce: How to Add a Custom Checkout Field
    Let’s imagine you want to add a custom checkout field (and not an additional billing or shipping field) on the WooCommerce Checkout page. For example, it might be a customer licence number – this has got nothing to do with billing and nothing to do with shipping. Ideally, this custom field could show above the […]
  • WooCommerce: Get Order Data (total, items, etc) From $order Object
    As a WooCommerce development freelancer, every day I repeat many coding operations that make me waste time. One of them is: “How to get ____ if I have the $order variable/object?“. For example, “How can I get the order total“? Or “How can I get the order items“? Or maybe the order dates, customer ID, […]
  • WooCommerce: Allow Users to Edit Processing Orders
    How can WooCommerce customers edit an order they just placed and paid for? I swear I looked on search engine results and other places before coming to the conclusion I needed to code this myself. For example, a user might want to change the delivery date (if you provide this on the checkout page). Or […]
  • WooCommerce: Create a Custom Order Status
    All WooCommerce orders go to either “processing”, “completed”, “on-hold” and other default order statuses based on the payment method and product type. Sometimes these statuses are not enough. For example, you might need to mark certain orders in a different way for tracking, filtering, exporting purposes. Or you might want to disable default emails by […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

4 thoughts on “WooCommerce: Create Order From Contact Form Submission

  1. Would it be possible to create an order for a specific product that is not free? … that has a price associated with it?

    1. Hello Patt, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. Hi Rodolfo. This could also be done for Gravity Form? I also wanted to ask you why you chose Fluent form. Do you consider it better than others in terms of customization for example?

    1. I’m 100% sure you can do it with GF, yes. I was already using the Fluent Forms / Fluent Support combo for ticketing, so I simply looked for a solution with the plugins I already have!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *