Add a new custom checkout field before billing details in Woocommerce?









up vote
1
down vote

favorite
1












I can add a set of custom fields to my WooCommerce checkout screen but need to move it above the 'Billing Details'.



How can that be done?



According to this official WooCommerce documentation, here is an example code to add extra custom checkout fields:



/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

function my_custom_checkout_field( $checkout )

echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';

woocommerce_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __('Enter something'),
), $checkout->get_value( 'my_field_name' ));

echo '</div>';











share|improve this question



























    up vote
    1
    down vote

    favorite
    1












    I can add a set of custom fields to my WooCommerce checkout screen but need to move it above the 'Billing Details'.



    How can that be done?



    According to this official WooCommerce documentation, here is an example code to add extra custom checkout fields:



    /**
    * Add the field to the checkout
    */
    add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

    function my_custom_checkout_field( $checkout )

    echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';

    woocommerce_form_field( 'my_field_name', array(
    'type' => 'text',
    'class' => array('my-field-class form-row-wide'),
    'label' => __('Fill in this field'),
    'placeholder' => __('Enter something'),
    ), $checkout->get_value( 'my_field_name' ));

    echo '</div>';











    share|improve this question

























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I can add a set of custom fields to my WooCommerce checkout screen but need to move it above the 'Billing Details'.



      How can that be done?



      According to this official WooCommerce documentation, here is an example code to add extra custom checkout fields:



      /**
      * Add the field to the checkout
      */
      add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

      function my_custom_checkout_field( $checkout )

      echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';

      woocommerce_form_field( 'my_field_name', array(
      'type' => 'text',
      'class' => array('my-field-class form-row-wide'),
      'label' => __('Fill in this field'),
      'placeholder' => __('Enter something'),
      ), $checkout->get_value( 'my_field_name' ));

      echo '</div>';











      share|improve this question















      I can add a set of custom fields to my WooCommerce checkout screen but need to move it above the 'Billing Details'.



      How can that be done?



      According to this official WooCommerce documentation, here is an example code to add extra custom checkout fields:



      /**
      * Add the field to the checkout
      */
      add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

      function my_custom_checkout_field( $checkout )

      echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';

      woocommerce_form_field( 'my_field_name', array(
      'type' => 'text',
      'class' => array('my-field-class form-row-wide'),
      'label' => __('Fill in this field'),
      'placeholder' => __('Enter something'),
      ), $checkout->get_value( 'my_field_name' ));

      echo '</div>';








      php wordpress woocommerce checkout hook-woocommerce






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 9 at 18:12









      LoicTheAztec

      83.2k125993




      83.2k125993










      asked May 9 at 15:35









      Simple Theory

      687




      687






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Updated: There is only one available hook before checkout billing fields that you can use to add custom fields in checkout form. Try this complete code:



          add_action( 'woocommerce_checkout_before_customer_details', 'custom_checkout_fields_before_billing_details', 20 );
          function custom_checkout_fields_before_billing_details()
          $domain = 'woocommerce';
          $checkout = WC()->checkout;

          echo '<div id="my_custom_checkout_field">';

          echo '<h3>' . __('My New Fields Section') . '</h3>';

          woocommerce_form_field( '_my_field_name', array(
          'type' => 'text',
          'label' => __('My 1st new field', $domain ),
          'placeholder' => __('Please fill in "my 1st new field"', $domain ),
          'class' => array('my-field-class form-row-wide'),
          'required' => true, // or false
          ), $checkout->get_value( '_my_field_name' ) );

          echo '</div>';


          // Custom checkout fields validation
          add_action( 'woocommerce_checkout_process', 'custom_checkout_field_process' );
          function custom_checkout_field_process()
          if ( isset($_POST['_my_field_name']) && empty($_POST['_my_field_name']) )
          wc_add_notice( __( 'Please fill in "My 1st new field".' ), 'error' );


          // Save custom checkout fields the data to the order
          add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_meta', 10, 2 );
          function custom_checkout_field_update_meta( $order, $data )
          if( isset($_POST['_my_field_name']) && ! empty($_POST['_my_field_name']) )
          $order->update_meta_data( '_my_field_name', sanitize_text_field( $_POST['_my_field_name'] ) );



          Code goes in function.php file of your active child theme (or active theme). Tested and works.



          You will get a new field section with yours new custom field (where you can have many also):



          enter image description here




          Official reference docs: Customizing checkout fields using actions and filters






          share|improve this answer






















          • Thank you! That is exactly what I needed. I've spent quite some time looking for this but just couldn't seem to find it anywhere else.
            – Simple Theory
            May 10 at 19:24

















          up vote
          1
          down vote













          WooCommerce uses hooks to display checkout page elements, such as the billing and shipping fields. You can use them to add your own custom content. You'll notice that the first line of code you posted contains woocommerce_after_order_notes which is one of those hooks. It defines the placement of the field.



          If you wanted to add a message before the Billing Details, you could do it with this hook: woocommerce_before_checkout_billing_form



          You can find a complete list of WooCommerce hooks at https://docs.woocommerce.com/wc-apidocs/hook-docs.html






          share|improve this answer




















          • Thanks for your input but that is not quite what I was looking for. I wanted to add a group of fields that are not associated with billing. With that hook it looks like it added the fields at the top, but still underneath the billing heading.
            – Simple Theory
            May 10 at 19:28










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50257147%2fadd-a-new-custom-checkout-field-before-billing-details-in-woocommerce%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          Updated: There is only one available hook before checkout billing fields that you can use to add custom fields in checkout form. Try this complete code:



          add_action( 'woocommerce_checkout_before_customer_details', 'custom_checkout_fields_before_billing_details', 20 );
          function custom_checkout_fields_before_billing_details()
          $domain = 'woocommerce';
          $checkout = WC()->checkout;

          echo '<div id="my_custom_checkout_field">';

          echo '<h3>' . __('My New Fields Section') . '</h3>';

          woocommerce_form_field( '_my_field_name', array(
          'type' => 'text',
          'label' => __('My 1st new field', $domain ),
          'placeholder' => __('Please fill in "my 1st new field"', $domain ),
          'class' => array('my-field-class form-row-wide'),
          'required' => true, // or false
          ), $checkout->get_value( '_my_field_name' ) );

          echo '</div>';


          // Custom checkout fields validation
          add_action( 'woocommerce_checkout_process', 'custom_checkout_field_process' );
          function custom_checkout_field_process()
          if ( isset($_POST['_my_field_name']) && empty($_POST['_my_field_name']) )
          wc_add_notice( __( 'Please fill in "My 1st new field".' ), 'error' );


          // Save custom checkout fields the data to the order
          add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_meta', 10, 2 );
          function custom_checkout_field_update_meta( $order, $data )
          if( isset($_POST['_my_field_name']) && ! empty($_POST['_my_field_name']) )
          $order->update_meta_data( '_my_field_name', sanitize_text_field( $_POST['_my_field_name'] ) );



          Code goes in function.php file of your active child theme (or active theme). Tested and works.



          You will get a new field section with yours new custom field (where you can have many also):



          enter image description here




          Official reference docs: Customizing checkout fields using actions and filters






          share|improve this answer






















          • Thank you! That is exactly what I needed. I've spent quite some time looking for this but just couldn't seem to find it anywhere else.
            – Simple Theory
            May 10 at 19:24














          up vote
          3
          down vote



          accepted










          Updated: There is only one available hook before checkout billing fields that you can use to add custom fields in checkout form. Try this complete code:



          add_action( 'woocommerce_checkout_before_customer_details', 'custom_checkout_fields_before_billing_details', 20 );
          function custom_checkout_fields_before_billing_details()
          $domain = 'woocommerce';
          $checkout = WC()->checkout;

          echo '<div id="my_custom_checkout_field">';

          echo '<h3>' . __('My New Fields Section') . '</h3>';

          woocommerce_form_field( '_my_field_name', array(
          'type' => 'text',
          'label' => __('My 1st new field', $domain ),
          'placeholder' => __('Please fill in "my 1st new field"', $domain ),
          'class' => array('my-field-class form-row-wide'),
          'required' => true, // or false
          ), $checkout->get_value( '_my_field_name' ) );

          echo '</div>';


          // Custom checkout fields validation
          add_action( 'woocommerce_checkout_process', 'custom_checkout_field_process' );
          function custom_checkout_field_process()
          if ( isset($_POST['_my_field_name']) && empty($_POST['_my_field_name']) )
          wc_add_notice( __( 'Please fill in "My 1st new field".' ), 'error' );


          // Save custom checkout fields the data to the order
          add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_meta', 10, 2 );
          function custom_checkout_field_update_meta( $order, $data )
          if( isset($_POST['_my_field_name']) && ! empty($_POST['_my_field_name']) )
          $order->update_meta_data( '_my_field_name', sanitize_text_field( $_POST['_my_field_name'] ) );



          Code goes in function.php file of your active child theme (or active theme). Tested and works.



          You will get a new field section with yours new custom field (where you can have many also):



          enter image description here




          Official reference docs: Customizing checkout fields using actions and filters






          share|improve this answer






















          • Thank you! That is exactly what I needed. I've spent quite some time looking for this but just couldn't seem to find it anywhere else.
            – Simple Theory
            May 10 at 19:24












          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          Updated: There is only one available hook before checkout billing fields that you can use to add custom fields in checkout form. Try this complete code:



          add_action( 'woocommerce_checkout_before_customer_details', 'custom_checkout_fields_before_billing_details', 20 );
          function custom_checkout_fields_before_billing_details()
          $domain = 'woocommerce';
          $checkout = WC()->checkout;

          echo '<div id="my_custom_checkout_field">';

          echo '<h3>' . __('My New Fields Section') . '</h3>';

          woocommerce_form_field( '_my_field_name', array(
          'type' => 'text',
          'label' => __('My 1st new field', $domain ),
          'placeholder' => __('Please fill in "my 1st new field"', $domain ),
          'class' => array('my-field-class form-row-wide'),
          'required' => true, // or false
          ), $checkout->get_value( '_my_field_name' ) );

          echo '</div>';


          // Custom checkout fields validation
          add_action( 'woocommerce_checkout_process', 'custom_checkout_field_process' );
          function custom_checkout_field_process()
          if ( isset($_POST['_my_field_name']) && empty($_POST['_my_field_name']) )
          wc_add_notice( __( 'Please fill in "My 1st new field".' ), 'error' );


          // Save custom checkout fields the data to the order
          add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_meta', 10, 2 );
          function custom_checkout_field_update_meta( $order, $data )
          if( isset($_POST['_my_field_name']) && ! empty($_POST['_my_field_name']) )
          $order->update_meta_data( '_my_field_name', sanitize_text_field( $_POST['_my_field_name'] ) );



          Code goes in function.php file of your active child theme (or active theme). Tested and works.



          You will get a new field section with yours new custom field (where you can have many also):



          enter image description here




          Official reference docs: Customizing checkout fields using actions and filters






          share|improve this answer














          Updated: There is only one available hook before checkout billing fields that you can use to add custom fields in checkout form. Try this complete code:



          add_action( 'woocommerce_checkout_before_customer_details', 'custom_checkout_fields_before_billing_details', 20 );
          function custom_checkout_fields_before_billing_details()
          $domain = 'woocommerce';
          $checkout = WC()->checkout;

          echo '<div id="my_custom_checkout_field">';

          echo '<h3>' . __('My New Fields Section') . '</h3>';

          woocommerce_form_field( '_my_field_name', array(
          'type' => 'text',
          'label' => __('My 1st new field', $domain ),
          'placeholder' => __('Please fill in "my 1st new field"', $domain ),
          'class' => array('my-field-class form-row-wide'),
          'required' => true, // or false
          ), $checkout->get_value( '_my_field_name' ) );

          echo '</div>';


          // Custom checkout fields validation
          add_action( 'woocommerce_checkout_process', 'custom_checkout_field_process' );
          function custom_checkout_field_process()
          if ( isset($_POST['_my_field_name']) && empty($_POST['_my_field_name']) )
          wc_add_notice( __( 'Please fill in "My 1st new field".' ), 'error' );


          // Save custom checkout fields the data to the order
          add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_meta', 10, 2 );
          function custom_checkout_field_update_meta( $order, $data )
          if( isset($_POST['_my_field_name']) && ! empty($_POST['_my_field_name']) )
          $order->update_meta_data( '_my_field_name', sanitize_text_field( $_POST['_my_field_name'] ) );



          Code goes in function.php file of your active child theme (or active theme). Tested and works.



          You will get a new field section with yours new custom field (where you can have many also):



          enter image description here




          Official reference docs: Customizing checkout fields using actions and filters







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 0:53

























          answered May 9 at 17:55









          LoicTheAztec

          83.2k125993




          83.2k125993











          • Thank you! That is exactly what I needed. I've spent quite some time looking for this but just couldn't seem to find it anywhere else.
            – Simple Theory
            May 10 at 19:24
















          • Thank you! That is exactly what I needed. I've spent quite some time looking for this but just couldn't seem to find it anywhere else.
            – Simple Theory
            May 10 at 19:24















          Thank you! That is exactly what I needed. I've spent quite some time looking for this but just couldn't seem to find it anywhere else.
          – Simple Theory
          May 10 at 19:24




          Thank you! That is exactly what I needed. I've spent quite some time looking for this but just couldn't seem to find it anywhere else.
          – Simple Theory
          May 10 at 19:24












          up vote
          1
          down vote













          WooCommerce uses hooks to display checkout page elements, such as the billing and shipping fields. You can use them to add your own custom content. You'll notice that the first line of code you posted contains woocommerce_after_order_notes which is one of those hooks. It defines the placement of the field.



          If you wanted to add a message before the Billing Details, you could do it with this hook: woocommerce_before_checkout_billing_form



          You can find a complete list of WooCommerce hooks at https://docs.woocommerce.com/wc-apidocs/hook-docs.html






          share|improve this answer




















          • Thanks for your input but that is not quite what I was looking for. I wanted to add a group of fields that are not associated with billing. With that hook it looks like it added the fields at the top, but still underneath the billing heading.
            – Simple Theory
            May 10 at 19:28














          up vote
          1
          down vote













          WooCommerce uses hooks to display checkout page elements, such as the billing and shipping fields. You can use them to add your own custom content. You'll notice that the first line of code you posted contains woocommerce_after_order_notes which is one of those hooks. It defines the placement of the field.



          If you wanted to add a message before the Billing Details, you could do it with this hook: woocommerce_before_checkout_billing_form



          You can find a complete list of WooCommerce hooks at https://docs.woocommerce.com/wc-apidocs/hook-docs.html






          share|improve this answer




















          • Thanks for your input but that is not quite what I was looking for. I wanted to add a group of fields that are not associated with billing. With that hook it looks like it added the fields at the top, but still underneath the billing heading.
            – Simple Theory
            May 10 at 19:28












          up vote
          1
          down vote










          up vote
          1
          down vote









          WooCommerce uses hooks to display checkout page elements, such as the billing and shipping fields. You can use them to add your own custom content. You'll notice that the first line of code you posted contains woocommerce_after_order_notes which is one of those hooks. It defines the placement of the field.



          If you wanted to add a message before the Billing Details, you could do it with this hook: woocommerce_before_checkout_billing_form



          You can find a complete list of WooCommerce hooks at https://docs.woocommerce.com/wc-apidocs/hook-docs.html






          share|improve this answer












          WooCommerce uses hooks to display checkout page elements, such as the billing and shipping fields. You can use them to add your own custom content. You'll notice that the first line of code you posted contains woocommerce_after_order_notes which is one of those hooks. It defines the placement of the field.



          If you wanted to add a message before the Billing Details, you could do it with this hook: woocommerce_before_checkout_billing_form



          You can find a complete list of WooCommerce hooks at https://docs.woocommerce.com/wc-apidocs/hook-docs.html







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 9 at 16:28









          Justin R.

          3421310




          3421310











          • Thanks for your input but that is not quite what I was looking for. I wanted to add a group of fields that are not associated with billing. With that hook it looks like it added the fields at the top, but still underneath the billing heading.
            – Simple Theory
            May 10 at 19:28
















          • Thanks for your input but that is not quite what I was looking for. I wanted to add a group of fields that are not associated with billing. With that hook it looks like it added the fields at the top, but still underneath the billing heading.
            – Simple Theory
            May 10 at 19:28















          Thanks for your input but that is not quite what I was looking for. I wanted to add a group of fields that are not associated with billing. With that hook it looks like it added the fields at the top, but still underneath the billing heading.
          – Simple Theory
          May 10 at 19:28




          Thanks for your input but that is not quite what I was looking for. I wanted to add a group of fields that are not associated with billing. With that hook it looks like it added the fields at the top, but still underneath the billing heading.
          – Simple Theory
          May 10 at 19:28

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50257147%2fadd-a-new-custom-checkout-field-before-billing-details-in-woocommerce%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          What does pagestruct do in Eviews?

          Dutch intervention in Lombok and Karangasem

          Channel Islands