Created
August 30, 2026 06:23
-
-
Save shameemreza/cb47ed37a426b63108ef12197b556ed4 to your computer and use it in GitHub Desktop.
Add a 'How did you hear about us?' dropdown to the WooCommerce block checkout (Additional Checkout Fields API, WC 8.9+)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Add a "How did you hear about us?" dropdown to the WooCommerce block checkout. | |
| * | |
| * Uses the Additional Checkout Fields API (WooCommerce 8.9+), which only works | |
| * with the block-based Checkout. Classic shortcode checkouts need the older | |
| * woocommerce_form_field approach instead. | |
| * | |
| * What it does: | |
| * - Shows an optional select field in the Additional information step of checkout. | |
| * - Saves the answer to the order automatically (meta key: _wc_other/my-store/lead-source). | |
| * - Shows the answer on the order confirmation page and on the admin order edit | |
| * screen (under the shipping details). No extra display code needed. | |
| * | |
| * Storage works on both HPOS and classic post storage. The value is saved through | |
| * the WC_Order CRUD layer, so WooCommerce writes it to whichever order table the | |
| * store uses. | |
| * | |
| * Notes: | |
| * - Express checkout flows (Apple Pay, Google Pay, WooPay) skip the checkout form, | |
| * so orders placed that way won't have an answer. | |
| * - The field and its admin display only render while this snippet is active. | |
| * Deactivating the snippet hides the field but never deletes saved data. | |
| * - Change "my-store" to your own namespace and edit the options to fit your store. | |
| * | |
| * Tested on WooCommerce 11.0.1 with the block checkout. | |
| */ | |
| defined( 'ABSPATH' ) || exit; | |
| add_action( 'woocommerce_init', function () { | |
| if ( ! function_exists( 'woocommerce_register_additional_checkout_field' ) ) { | |
| return; | |
| } | |
| woocommerce_register_additional_checkout_field( | |
| array( | |
| 'id' => 'my-store/lead-source', | |
| 'label' => 'How did you hear about us?', | |
| 'location' => 'order', | |
| 'type' => 'select', | |
| 'required' => false, | |
| 'options' => array( | |
| array( 'value' => 'google', 'label' => 'Google' ), | |
| array( 'value' => 'facebook', 'label' => 'Facebook' ), | |
| array( 'value' => 'instagram', 'label' => 'Instagram' ), | |
| array( 'value' => 'podcast', 'label' => 'Podcast' ), | |
| array( 'value' => 'trade_show', 'label' => 'Trade Show' ), | |
| array( 'value' => 'newsletter', 'label' => 'Newsletter' ), | |
| array( 'value' => 'word_of_mouth', 'label' => 'Word-of-mouth' ), | |
| array( 'value' => 'other', 'label' => 'Other' ), | |
| ), | |
| ) | |
| ); | |
| } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment