AI Verified

Name

WooCommerce Checkout How Did You Hear About Us

About

Keep in mind, the longer the checkout process, the more friction there is for the user to pay and become a customer.We’d recommend asking the user how they heard about your store on the thank you page instead to prevent harming the conversion rate.However, due to popular demand, this snippet implements a custom select checkout field which asks the user prior to purchasing and saves the answer as order meta data.Specifically, a new custom checkout field is generated with a “select” data type using the woocommerce_form_field() function.Select options (drop down list items) are then populated with a key and a value as part of an array.Notice how we’ve made the “required” parameter true. All this does is show an asterisk. We add custom validation after which actually prevents the user form checking out if the field is not populated. If the field is not populated, a custom error message is shown.Finally, the selection is stored as order meta data so you can refer back to the answer when looking at the orders.

Language

PHP

Rating

Voted: 2 by 2 user(s)

How to Setup Snippet

Simply copy the pre-coded solution to your active child theme’s functions.php or preferably the Code Snippets Plugin.

Link for further information:

The author has provided the following URL that may be helpful to setting up or using this snippet:

https://www.ecommercehints.com/woocommerce-checkout-how-did-you-hear-about-us

Codevault

ecommercehints

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

Website/ Profile URL:

https://www.ecommercehints.com/

History

Last modified:

06/07/2024

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

WooCommerce Checkout How Did You Hear About Us

 
                    
1/**
2 * Snippet Name: WooCommerce Checkout How Did You Hear About Us
3 * Snippet Author: ecommercehints.com
4 */
5 
6// Create the custom select field in the billing section of the checkout form
7add_action( 'woocommerce_after_checkout_billing_form', 'ecommercehints_checkout_select_field' );
8function ecommercehints_checkout_select_field($checkout) {
9woocommerce_form_field(
10 'how_did_you_hear',
11 array(
12 'type' => 'select',
13 'required' => true, // Shows an asterisk if true (*)
14 'label' => 'How Did You Hear About Us?',
15 'options' => array(
16 '' => 'Please select...',
17 'word-of-mouth' => 'Word of mouth',
18 'google-search' => 'Google Search',
19 'social-media' => 'Social Media',
20 'ecommercehints' => 'eCommerceHints.com'
21 )
22 ),
23 ( isset($_POST['how_did_you_hear']) ? $_POST['how_did_you_hear'] : '' )
24 );
25}
26 
27// Show an error message of field is not populated
28add_action('woocommerce_checkout_process', 'ecommercehints_custom_checkout_select_field_validation');
29function ecommercehints_custom_checkout_select_field_validation() {
30 if (empty( $_POST['how_did_you_hear'] ) ) {
31 wc_add_notice( 'We would really like to know how you heard about us.', 'error' );
32 }
33}
34 
35// Save the custom field data as order meta
36add_action( 'woocommerce_checkout_update_order_meta', 'ecommercehints_save_custom_checkout_select_field' );
37function ecommercehints_save_custom_checkout_select_field( $order_id ){
38 if( !empty( $_POST['how_did_you_hear'] ) ) {
39 update_post_meta( $order_id, 'how_did_you_hear', sanitize_text_field( $_POST['how_did_you_hear'] ) );
40 }
41}

2

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

0

Remove WooCommerce breadcrumbs on all pages

Added: 1 year ago

Last Updated: 1 year ago

WooCommerce

AI Verified

2

Add Alphabetical Sorting to Woocommerce

Added: 1 year ago

Last Updated: 2 weeks ago

Sorts products alphabetically in Woocommerce

WooCommerce

AI Verified

0

Remove WooCommerce Catalog "Orderby" dropdown options

Added: 1 year ago

Last Updated: 1 year ago

Remove the "menu_order", "popularity", "rating", and "date" options from the orderby dropdown in the WooCommerce catalog

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WooCommerce

AI Verified

6

WooCommerce Show Number Of People Viewing Product

Added: 1 year ago

Last Updated: 2 weeks ago

We want to make this solution absolutely clear from the outset. This snippet does NOT generate the actual number of people viewing the product. It shows a randomly generated number in the range of two...

WooCommerce

AI Verified

5

WooCommerce Show Number Of Units Sold On Product Page

Added: 1 year ago

Last Updated: 2 weeks ago

Showing the number of product units sold on the single product page is a great way to introduce Fear Of Missing Out (FOMO). You can highlight how many products are sold to let customers know a product...

WooCommerce

AI Verified

3

WooCommerce Add Content Under The Proceed To Checkout Button On The Cart Page

Added: 1 year ago

Last Updated: 2 weeks ago

How many times have you bought something online and seen trust symbols near the proceed to checkout button on the cart page? “30 Day Money Back Guarantee” or “Secure Checkout” are popular examples. By...