$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to be able to edit custom address fields in WooCommerce admin?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to be able to edit custom address fields in WooCommerce admin?

matteradmin8PV0评论

I'm Dutch and in The Netherlands we use a different address format than in the US. We'd have to spit the street name and the house number. In order to fix that I used "address line 1" for 'Street name' and added a new custom checkout field called "house number". When a user orders something everything works fine at checkout. But in the WordPress/Woocommerce admin I'm having difficulties.

When I'm viewing an order everything looks fine. That's because I have a new address format:

add_filter( 'woocommerce_localisation_address_formats', 'new_address_formats' );

function new_address_formats( $formats ) {
    $formats['NL'] = "{name}\n{company}\n{address_1} {billing_house_number} {shipping_house_number}\n{postcode} {city}\n{country}";
    return $formats;
}

But the problem comes when I click on the "edit address" icon. Address line 1 isn't called 'Street name', the House Number field isn't present at all and Address line 2 needs to be unset:

In order to be able to edit the given House Number I added a custom function in my functions.php:

function editable_order_meta_shipping( $order ){

$house_number = get_post_meta( $order->get_id(), '_shipping_house_number', true );
//unset($shipping_fields['address_2']);
?>
<div class="address">
    <p<?php if ( empty($house_number) ) echo ' class="none_set"' ?>>
        <strong>House number:</strong>
        <?php echo ( !empty( $house_number ) ) ? $house_number : 'unknown' ?>
    </p>
</div>
<div class="edit_address"><?php
    woocommerce_wp_text_input( array( 
        'id' => '_shipping_house_number',
        'label' => 'House number', 
        'wrapper_class' => 'form-field',
        'class' => '_shipping_house_number',
        'value' => $house_number
    ) ); ?>
</div>

<?php
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'editable_order_meta_shipping' );


function save_shipping_details( $ord_id ){
    update_post_meta( $ord_id, '_shipping_house_number', wc_clean( $_POST[ '_shipping_house_number' ] ) );
}
add_action( 'woocommerce_process_shop_order_meta', 'save_shipping_details' );

This adds a field with "House number" that's editable:

But I'm not able to find a solution to rename "Address line 1" label, to remove "Address line 2" completely and to reorder the fields so that "House number" is next to the "Address Line 1/Street Name" field.

Current situation:

What I would like to have:

Are you able to help/come up with a solution?

Thanks in advance!

Post a comment

comment list (0)

  1. No comments so far