Displaying custom checkout field value in Woocommerce admin order pages












2














In Woocommerce, I used Add a new custom checkout field before billing details in Woocommerce? from @LoicTheAztec that works perfectly.



I'm having trouble displaying the information in the admin order page.



Then I tried using Add order metadata to WooCommerce admin order overview, making some changes in the code to match with my needs:



// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){ ?>
<div class="order_data_column">
<h4><?php _e( 'Extra Details' ); ?></h4>
<?php
echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_my_field_name', true ) . '</p>';?>
</div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );?>


Extra Details and Some field: appears, but the custom field value isn't displayed.



I'm still trying to figure out what I did wrong here. Any suggestions on where I should look?










share|improve this question





























    2














    In Woocommerce, I used Add a new custom checkout field before billing details in Woocommerce? from @LoicTheAztec that works perfectly.



    I'm having trouble displaying the information in the admin order page.



    Then I tried using Add order metadata to WooCommerce admin order overview, making some changes in the code to match with my needs:



    // display the extra data in the order admin panel
    function kia_display_order_data_in_admin( $order ){ ?>
    <div class="order_data_column">
    <h4><?php _e( 'Extra Details' ); ?></h4>
    <?php
    echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_my_field_name', true ) . '</p>';?>
    </div>
    <?php }
    add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );?>


    Extra Details and Some field: appears, but the custom field value isn't displayed.



    I'm still trying to figure out what I did wrong here. Any suggestions on where I should look?










    share|improve this question



























      2












      2








      2







      In Woocommerce, I used Add a new custom checkout field before billing details in Woocommerce? from @LoicTheAztec that works perfectly.



      I'm having trouble displaying the information in the admin order page.



      Then I tried using Add order metadata to WooCommerce admin order overview, making some changes in the code to match with my needs:



      // display the extra data in the order admin panel
      function kia_display_order_data_in_admin( $order ){ ?>
      <div class="order_data_column">
      <h4><?php _e( 'Extra Details' ); ?></h4>
      <?php
      echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_my_field_name', true ) . '</p>';?>
      </div>
      <?php }
      add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );?>


      Extra Details and Some field: appears, but the custom field value isn't displayed.



      I'm still trying to figure out what I did wrong here. Any suggestions on where I should look?










      share|improve this question















      In Woocommerce, I used Add a new custom checkout field before billing details in Woocommerce? from @LoicTheAztec that works perfectly.



      I'm having trouble displaying the information in the admin order page.



      Then I tried using Add order metadata to WooCommerce admin order overview, making some changes in the code to match with my needs:



      // display the extra data in the order admin panel
      function kia_display_order_data_in_admin( $order ){ ?>
      <div class="order_data_column">
      <h4><?php _e( 'Extra Details' ); ?></h4>
      <?php
      echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_my_field_name', true ) . '</p>';?>
      </div>
      <?php }
      add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );?>


      Extra Details and Some field: appears, but the custom field value isn't displayed.



      I'm still trying to figure out what I did wrong here. Any suggestions on where I should look?







      php wordpress woocommerce backend orders






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 1:01









      LoicTheAztec

      84.7k136095




      84.7k136095










      asked Nov 11 '18 at 23:49









      J. Frenk

      112




      112
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Update 3: Try the following lightly revisited 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'] ) );
          }

          // Display the extra data in the order admin panel
          add_action( 'woocommerce_admin_order_data_after_order_details', 'display_order_data_in_admin', 10, 1 );
          function display_order_data_in_admin( $order ){
          if( $value = $order->get_meta( '_my_field_name' ) ) {
          echo '<div class="order_data_column">
          <h4>' . __( "Extra Details", "woocommerce" ) . '</h4>
          <p><strong>' . __( 'Some field', "woocommerce" ) . ':</strong> ' . $value . '</p>
          </div>';
          }
          }


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






          share|improve this answer























          • I replaced the code with this, everything looks fine but nothing appears in the Admin order page- it now appears totally stock. I'm going to try to search through post data in the db to see if information I enter into the field is even being saved- maybe I have a wonky install
            – J. Frenk
            Nov 12 '18 at 0:43










          • @J.Frenk Just re-updated (3), I have make some other changes in the code (changed all meta_keys with an underscore on start as it's the best way for custom fields)… try it again please. If this answer is answering your question, you could please accept the answer, Thank you.
            – LoicTheAztec
            Nov 12 '18 at 0:57













          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',
          autoActivateHeartbeat: false,
          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%2f53254412%2fdisplaying-custom-checkout-field-value-in-woocommerce-admin-order-pages%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Update 3: Try the following lightly revisited 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'] ) );
          }

          // Display the extra data in the order admin panel
          add_action( 'woocommerce_admin_order_data_after_order_details', 'display_order_data_in_admin', 10, 1 );
          function display_order_data_in_admin( $order ){
          if( $value = $order->get_meta( '_my_field_name' ) ) {
          echo '<div class="order_data_column">
          <h4>' . __( "Extra Details", "woocommerce" ) . '</h4>
          <p><strong>' . __( 'Some field', "woocommerce" ) . ':</strong> ' . $value . '</p>
          </div>';
          }
          }


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






          share|improve this answer























          • I replaced the code with this, everything looks fine but nothing appears in the Admin order page- it now appears totally stock. I'm going to try to search through post data in the db to see if information I enter into the field is even being saved- maybe I have a wonky install
            – J. Frenk
            Nov 12 '18 at 0:43










          • @J.Frenk Just re-updated (3), I have make some other changes in the code (changed all meta_keys with an underscore on start as it's the best way for custom fields)… try it again please. If this answer is answering your question, you could please accept the answer, Thank you.
            – LoicTheAztec
            Nov 12 '18 at 0:57


















          1














          Update 3: Try the following lightly revisited 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'] ) );
          }

          // Display the extra data in the order admin panel
          add_action( 'woocommerce_admin_order_data_after_order_details', 'display_order_data_in_admin', 10, 1 );
          function display_order_data_in_admin( $order ){
          if( $value = $order->get_meta( '_my_field_name' ) ) {
          echo '<div class="order_data_column">
          <h4>' . __( "Extra Details", "woocommerce" ) . '</h4>
          <p><strong>' . __( 'Some field', "woocommerce" ) . ':</strong> ' . $value . '</p>
          </div>';
          }
          }


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






          share|improve this answer























          • I replaced the code with this, everything looks fine but nothing appears in the Admin order page- it now appears totally stock. I'm going to try to search through post data in the db to see if information I enter into the field is even being saved- maybe I have a wonky install
            – J. Frenk
            Nov 12 '18 at 0:43










          • @J.Frenk Just re-updated (3), I have make some other changes in the code (changed all meta_keys with an underscore on start as it's the best way for custom fields)… try it again please. If this answer is answering your question, you could please accept the answer, Thank you.
            – LoicTheAztec
            Nov 12 '18 at 0:57
















          1












          1








          1






          Update 3: Try the following lightly revisited 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'] ) );
          }

          // Display the extra data in the order admin panel
          add_action( 'woocommerce_admin_order_data_after_order_details', 'display_order_data_in_admin', 10, 1 );
          function display_order_data_in_admin( $order ){
          if( $value = $order->get_meta( '_my_field_name' ) ) {
          echo '<div class="order_data_column">
          <h4>' . __( "Extra Details", "woocommerce" ) . '</h4>
          <p><strong>' . __( 'Some field', "woocommerce" ) . ':</strong> ' . $value . '</p>
          </div>';
          }
          }


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






          share|improve this answer














          Update 3: Try the following lightly revisited 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'] ) );
          }

          // Display the extra data in the order admin panel
          add_action( 'woocommerce_admin_order_data_after_order_details', 'display_order_data_in_admin', 10, 1 );
          function display_order_data_in_admin( $order ){
          if( $value = $order->get_meta( '_my_field_name' ) ) {
          echo '<div class="order_data_column">
          <h4>' . __( "Extra Details", "woocommerce" ) . '</h4>
          <p><strong>' . __( 'Some field', "woocommerce" ) . ':</strong> ' . $value . '</p>
          </div>';
          }
          }


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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 '18 at 0:59

























          answered Nov 12 '18 at 0:07









          LoicTheAztec

          84.7k136095




          84.7k136095












          • I replaced the code with this, everything looks fine but nothing appears in the Admin order page- it now appears totally stock. I'm going to try to search through post data in the db to see if information I enter into the field is even being saved- maybe I have a wonky install
            – J. Frenk
            Nov 12 '18 at 0:43










          • @J.Frenk Just re-updated (3), I have make some other changes in the code (changed all meta_keys with an underscore on start as it's the best way for custom fields)… try it again please. If this answer is answering your question, you could please accept the answer, Thank you.
            – LoicTheAztec
            Nov 12 '18 at 0:57




















          • I replaced the code with this, everything looks fine but nothing appears in the Admin order page- it now appears totally stock. I'm going to try to search through post data in the db to see if information I enter into the field is even being saved- maybe I have a wonky install
            – J. Frenk
            Nov 12 '18 at 0:43










          • @J.Frenk Just re-updated (3), I have make some other changes in the code (changed all meta_keys with an underscore on start as it's the best way for custom fields)… try it again please. If this answer is answering your question, you could please accept the answer, Thank you.
            – LoicTheAztec
            Nov 12 '18 at 0:57


















          I replaced the code with this, everything looks fine but nothing appears in the Admin order page- it now appears totally stock. I'm going to try to search through post data in the db to see if information I enter into the field is even being saved- maybe I have a wonky install
          – J. Frenk
          Nov 12 '18 at 0:43




          I replaced the code with this, everything looks fine but nothing appears in the Admin order page- it now appears totally stock. I'm going to try to search through post data in the db to see if information I enter into the field is even being saved- maybe I have a wonky install
          – J. Frenk
          Nov 12 '18 at 0:43












          @J.Frenk Just re-updated (3), I have make some other changes in the code (changed all meta_keys with an underscore on start as it's the best way for custom fields)… try it again please. If this answer is answering your question, you could please accept the answer, Thank you.
          – LoicTheAztec
          Nov 12 '18 at 0:57






          @J.Frenk Just re-updated (3), I have make some other changes in the code (changed all meta_keys with an underscore on start as it's the best way for custom fields)… try it again please. If this answer is answering your question, you could please accept the answer, Thank you.
          – LoicTheAztec
          Nov 12 '18 at 0:57




















          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%2f53254412%2fdisplaying-custom-checkout-field-value-in-woocommerce-admin-order-pages%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







          Popular posts from this blog

          Full-time equivalent

          Bicuculline

          さくらももこ