3
votes

I have created the following custom checkout field for WooCommerce, based on all the examples that can be found.

  • Billing house number billing_houseno
  • Shipping house number shipping_houseno

Output shows an address field (street) and house number field. Works perfectly fine. Stores data nicely in a custom house number field and Address works like it normally does - of course.

I'd like to merge the following fields in one:

  • billing_address_1 and billing_houseno in billing_address_1 existing field.
  • shipping_address_1 and shipping_houseno in shipping_address_1 existing field.

So it will combine both fields in one database field.

Is there a way to store both fields in one existing field?

3

3 Answers

1
votes

Update:

In your comment code link, there is some errors in your code:

  1. In your bones_add_field_and_reorder_fields function, you are unsetting billing and shipping company, but after few lines down, you are trying to set those values in:
$newfields['billing']['billing_company']   = $fields['billing']['billing_company'];
$newfields['shipping']['shipping_company'] = $fields['shipping']['shipping_company'];

And it's throwing some errors
You should not unset 'billing_company' and 'shipping_company'.

  1. Everywhere you are using $order->id and it should be replaced by $order->get_id().
  1. I have tested everything with your updated code and my updated function below. It works

Using a custom function hooked in woocommerce_checkout_update_order_meta action hook, you will be able to merge the values.

add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta', 10, 2 );
function custom_checkout_field_update_order_meta( $order_id ) {
    $separator = '. '; // Separator for merged fields

    // Billing fields
    $billing_address_1 = sanitize_text_field( $_POST['billing_address_1'] );
    $billing_houseno   = sanitize_text_field( $_POST['billing_houseno'] );

    if( ! ( empty($billing_address_1) && empty($billing_houseno) ) )
        update_post_meta( $order_id, '_billing_address_1', $billing_address_1 . $separator . $billing_houseno );

    $shipping_address_1 = sanitize_text_field( $_POST['shipping_address_1'] );
    $shipping_houseno   = sanitize_text_field( $_POST['shipping_houseno'] );

    if( ! ( empty($shipping_address_1) && empty($shipping_houseno) ) )
        update_post_meta( $order_id, '_shipping_address_1', $shipping_address_1 . $separator . $shipping_houseno );
}

Code goes in function.php file of the active child theme (or active theme).

This time, this is tested and works.

0
votes

Try this

add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta',10,2);

function customise_checkout_field_update_order_meta($order_id, $posted_data)
{
//write your code here to update custom data.
}
0
votes
<?php
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['billing_houseno'] ) ) {
        $billing_houseno = $_POST['billing_houseno'];
        $billing_address_1 = $_POST['billing_address_1'];
        $new_billing_address = $billing_houseno.'.'.$billing_address_1;
        update_post_meta( $order_id, '_billing_address_1', $new_billing_address );
    }
    if(!empty($_POST['shipping_houseno'])){
        $shipping_houseno = $_POST['shipping_houseno'];
        $shipping_address_1 = $_POST['shipping_address_1'];
        $new_shipping_address = $shipping_houseno.'.'.$shipping_address_1;
        update_post_meta( $order_id, '_shipping_address_1', $new_shipping_address );    
    }
}
?>