0

I am trying to remove the field and add in a different location but it is not saving in billing detail. Please guide me how can i fix this.

Code

add_filter('woocommerce_checkout_fields', 'quadlayers_remove_checkout_fields');
function quadlayers_remove_checkout_fields($fields)
{
    unset($fields['billing']['billing_first_name']);
    return $fields;
}


add_action('woocommerce_checkout_order_review', 'checkout_review_order_custom_field');

function checkout_review_order_custom_field()
{
    woocommerce_form_field('billing_first_name', array(
        'type'     => 'text',
        'class'    => array(''),
        'label'    => __('Name', 'sidesmedia'),
        'placeholder' => 'Card Holder Name',
        'required' => true,
    ));
}

Any solution appreciated!

Robin Singh
  • 1,565
  • 1
  • 13
  • 38
  • If you unset the field it will be not saved automatically even if you use the same name in a new field. You'll have to write a save and validation functions for that new field. – Vijay Hardaha Aug 25 '22 at 13:43
  • Can you please write or help me @VijayHardaha – Robin Singh Aug 25 '22 at 13:44
  • You can check this [article](https://www.businessbloomer.com/woocommerce-add-custom-checkout-field-php/) or google search `How to Add a Custom Checkout Field` you'll find so many article for the save and validation code of extra checkout field. – Vijay Hardaha Aug 25 '22 at 13:49

1 Answers1

0

I collected my field like this:

 add_action('woocommerce_checkout_create_order', 'order_meta', 22, 1 );
 function order_meta( $order ) {

     if ( isset($_POST['field_name']) ) {
         $order->update_meta_data( 'field_name', $_POST['field_name'] ); 
     }
 }

To show it in the admin section under the billing address:

function show_data($order){
    echo "Meta field: " . get_post_meta( $order->id, 'field_name', true );
} 
add_action( 'woocommerce_admin_order_data_after_billing_address','show_data', 10, 1 );
IronCanTaco
  • 101
  • 1
  • 12
  • Your answer is unsafe, as you're inserting unsanitized post data into the database. In the second part, you're also echoing output without escaping it. Additionally, WC objects shouldn't be accessed directly, but rather you should use `$order->get_id()` You can reference this https://stackoverflow.com/questions/39401393/how-to-get-woocommerce-order-details – Howard E Aug 25 '22 at 15:33