Custom Fields Stopped Working

We're shifting our Forum based support to a more dedicated support system!

We'll be closing our Forum support from 10th June, 2020 and move to Email Support assistance.

  • If you are a WCFM premium add-ons user, contact us- here
  • Want to know more before buying our add-ons? Send Pre sale queries- here
  • If you are a WCFM free user, please open a support ticket at WordPress.org
  • For WCFM App related queries, reach us- here
From now the forum will be read-only!

Viewing 3 reply threads
  • Author
    Posts
    • #117798
      SSC
      Participant

      Hello, I have some custom fields on my checkout page.
      They suddenly stopped working and I can not figure out why. I have spent all day trying to figure this out. There are no errors in the logs.
      The fields are no longer saving as post_meta.

      add_action('woocommerce_before_checkout_form', 'add_custom_checkout_select_field2');
      function add_custom_checkout_select_field2( $checkout ) {
      	global $woocommerce, $post ;     
      foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {
      	$product = $values['data'];
      		echo '<div id="customise_checkout_field">';
      		woocommerce_form_field('test_field', array(
              'type' => 'text',
              'class' => array(
              'player-name form-row-wide'
              ),
              'label' => __('test_field') ,
              'placeholder' => __('test_field') ,
      		),
      		$checkout->get_value('test_field'));
      		echo '</div>';
      }
      }
      add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta2' );
      function my_custom_checkout_field_update_order_meta2( $order_id ) {
          if ( !empty( $_POST['test_field'] )) {
      		update_post_meta( $order_id, 'test_field', sanitize_text_field( $_POST['test_field'] ) );
          }
      }
      add_filter( 'woocommerce_order_details_after_order_table' , 'my_custom_checkout_field_display_admin_order_meta2', 20, 1 ); // Front
      add_filter( 'woocommerce_email_order_details' , 'my_custom_checkout_field_display_admin_order_meta2', 20, 1 ); // Front
      //Admin
      add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta2', 10, 1 );
      function my_custom_checkout_field_display_admin_order_meta2($order){
      	$test_field = get_post_meta( $order->get_id(), 'test_field', true );
      	
      	if ( $test_field ) {
          echo '<p><strong>'.__('test_field').':</strong> ' . get_post_meta( $order->get_id(), 'test_field', true ) . '</p>';
      	}
      }
    • #120276
      Sushobhan
      Keymaster

      Hi,
      What you want to achieve with this snippet? Your wrote “They suddenly stopped working”, how was it working before? Did you change anything or update plugin/WP version?
      The snippet doesn’t seem right to me. Please tell me in detail what is your actual requirement. Then only, I might be able to help you out.
      Thanks!

    • #120289
      SSC
      Participant

      I also mentioned, “The fields are no longer saving as post_meta”.
      I suppose I should have added more info, it has been quite hectic over here and it was very late when I posted this xD.
      I believe it stopped working after the latest update. I got back around to this today and figure out what the issue was.
      Changed “woocommerce_before_checkout_form” to “woocommerce_checkout_before_customer_details” and added “$checkout = WC()->checkout();” everything works fine now, and I will no longer need help on this issue.

    • #120291
      Sushobhan
      Keymaster

      Glad you figure it out 🙂

Viewing 3 reply threads
  • You must be logged in to reply to this topic.