Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //* Add select field to the checkout page
- add_action('woocommerce_before_order_notes', 'wps_add_select_checkout_field');
- function wps_add_select_checkout_field( $checkout ) {
- echo '<h2>'.__('How did you hear about us?').'</h2>';
- woocommerce_form_field( 'hearabout', array(
- 'type' => 'select',
- 'class' => array( 'wps-drop' ),
- 'options' => array(
- 'blank' => __( 'Select an option', 'wps' ),
- 'google search' => __( 'Google Search', 'wps' ),
- 'social media' => __( 'Social Media', 'wps' ),
- 'friend or family' => __( 'Friend or family', 'wps' ),
- 'other' => __( 'Other', 'wps' )
- )
- ),
- $checkout->get_value( 'hearabout' ));
- }
- //* Process the checkout
- add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
- function wps_select_checkout_field_process() {
- global $woocommerce;
- // Check if set, if its not set add an error.
- if ($_POST['hearabout'] == "blank")
- wc_add_notice( '<strong>Please select where you heard about us.</strong>', 'error' );
- }
- //* Update the order meta with field value
- add_action('woocommerce_checkout_update_order_meta', 'wps_select_checkout_field_update_order_meta');
- function wps_select_checkout_field_update_order_meta( $order_id ) {
- if ($_POST['hearabout']) update_post_meta( $order_id, 'hearabout', esc_attr($_POST['hearabout']));
- }
- //* Display field value on the order edition page
- add_action( 'woocommerce_admin_order_data_after_billing_address', 'wps_select_checkout_field_display_admin_order_meta', 10, 1 );
- function wps_select_checkout_field_display_admin_order_meta($order){
- echo '<p><strong>'.__('Heard about us at').':</strong> ' . get_post_meta( $order->id, 'hearabout', true ) . '</p>';
- }
- //* Add selection field value to emails
- add_filter('woocommerce_email_order_meta_keys', 'wps_select_order_meta_keys');
- function wps_select_order_meta_keys( $keys ) {
- $keys['Hearabout:'] = 'hearabout';
- return $keys;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement