Menu Close

WooCommerce: Create Order From Contact Form Submission

default

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!

View Source
Posted in WooCommerce Tips