SHARE
TWEET

Untitled

a guest Oct 21st, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //* Add select field to the checkout page
  2. add_action('woocommerce_before_order_notes', 'wps_add_select_checkout_field');
  3. function wps_add_select_checkout_field( $checkout ) {
  4.     echo '<h2>'.__('How did you hear about us?').'</h2>';
  5.     woocommerce_form_field( 'hearabout', array(
  6.         'type'          => 'select',
  7.         'class'         => array( 'wps-drop' ),
  8.         'options'       => array(
  9.             'blank'     => __( 'Select an option', 'wps' ),
  10.             'google search' => __( 'Google Search', 'wps' ),
  11.             'social media'  => __( 'Social Media', 'wps' ),
  12.             'friend or family'  => __( 'Friend or family', 'wps' ),
  13.             'other'     => __( 'Other', 'wps' )
  14.         )
  15.  ),
  16.     $checkout->get_value( 'hearabout' ));
  17. }
  18.  
  19. //* Process the checkout
  20.  add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
  21.  function wps_select_checkout_field_process() {
  22.     global $woocommerce;
  23.     // Check if set, if its not set add an error.
  24.     if ($_POST['hearabout'] == "blank")
  25.      wc_add_notice( '<strong>Please select where you heard about us.</strong>', 'error' );
  26.  }
  27.  
  28. //* Update the order meta with field value
  29.  add_action('woocommerce_checkout_update_order_meta', 'wps_select_checkout_field_update_order_meta');
  30.  function wps_select_checkout_field_update_order_meta( $order_id ) {
  31.    if ($_POST['hearabout']) update_post_meta( $order_id, 'hearabout', esc_attr($_POST['hearabout']));
  32.  }
  33.  
  34. //* Display field value on the order edition page
  35. add_action( 'woocommerce_admin_order_data_after_billing_address', 'wps_select_checkout_field_display_admin_order_meta', 10, 1 );
  36. function wps_select_checkout_field_display_admin_order_meta($order){
  37.     echo '<p><strong>'.__('Heard about us at').':</strong> ' . get_post_meta( $order->id, 'hearabout', true ) . '</p>';
  38. }
  39. //* Add selection field value to emails
  40. add_filter('woocommerce_email_order_meta_keys', 'wps_select_order_meta_keys');
  41. function wps_select_order_meta_keys( $keys ) {
  42.     $keys['Hearabout:'] = 'hearabout';
  43.     return $keys;
  44.    
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top