WordPress.org

Make WordPress Core

Opened 6 months ago

Closed 6 months ago

Last modified 6 months ago

#50407 closed defect (bug) (invalid)

how to edit and save custom address fields for Woocomerce My Account/Edit-address/billing page?

Reported by: ponnarasiaccu Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: Cc:

Description

for showing custom fields on myaccount/edit-address/billing page

add_action( 'woocommerce_after_edit_account_address_form', 'action_woocommerce_after_edit_account_address_form', 10, 1 );
function action_woocommerce_after_edit_account_address_form( $array ){ ?>

<div class="u-column1 col-1 woocommerce-Address">
        <header class="woocommerce-Address-title title">
    <h3>Order Placed Details</h3>
</header>
    <?php 
    $current_user_id = get_current_user_id();
    $order_state_id =  get_user_meta( $current_user_id, 'billing_custom_state', true );
    global $wpdb;
    $order_State = $wpdb->get_results( "select * from wp_posts where ID='".$order_state_id."' order by ID desc");
    foreach ($order_State as $order_State_value) 
    { 
        $order_billing_state = $order_State_value->post_title;
    }
    //get location value from database start
    $order_location_id =  get_user_meta( $current_user_id, 'billing_custom_location', true );
    global $wpdb;
    $order_location = $wpdb->get_results( $wpdb->prepare( "select * from wp_postmeta where meta_id='".$order_location_id."' " ) );
    if ( $order_location ) 
    {
        foreach ($order_location as $order_location_value) 
        { 
            //replace email id from address start
            //test string for checking email
            $test_patt ="/(?:[a-z0-9!#$%&'*+=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/";
            $replacement = "";
            $order_billing_location = preg_replace($test_patt, $replacement, $order_location_value->meta_value);
        }
    }
    ?>
    <address>
        State: <?php echo $order_billing_state ?><br>
        City:  <?php echo get_user_meta( $current_user_id, 'billing_custom_city', true ); ?><br>
        Location: <?php echo $order_billing_location; ?><br>
  </address>
    
</div>
<?php }

for update custom address field into database

/**
 * Update the user meta with field value
 **/
add_action( 'woocommerce_customer_save_address', 'action_woocommerce_customer_save_address', 10, 2 ); 

//$load_address = 'billing';

function action_woocommerce_customer_save_address($user_id, $load_address) {


    $order_state_id =  get_user_meta( $user_id, 'billing_custom_state', true );
    $order_city_id =  get_user_meta( $user_id, 'billing_custom_city', true );
    $order_location_id =  get_user_meta( $user_id, 'billing_custom_location', true );

    update_user_meta( $user_id, 'billing_custom_state', esc_attr($_POST['billing_custom_state']), $order_state_id );
    update_user_meta( $user_id, 'billing_custom_city', esc_attr($_POST['billing_custom_city']), $order_city_id );
    update_user_meta( $user_id, 'billing_custom_location', esc_attr($_POST['billing_custom_location']), $order_location_id );

    //update_post_meta( $post_id, '_billing_custom_state', esc_attr($_POST['billing_custom_state']), $order_state_id );*/

    $current_user_id = get_current_user_id();
    $customer_meta  = get_user_meta( $current_user_id );
    $customer_orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => $current_user_id,
        'post_type'   => wc_get_order_types(), //shop_order
        'post_status' => array_keys( wc_get_order_statuses() ) //wc-processing
    ) );

    foreach( $customer_orders as $order ) {

        update_post_meta( $order->ID, '_billing_custom_state', esc_attr($_POST['billing_custom_state']), $customer_meta['billing_custom_state'][0] );

        update_post_meta( $order->ID, '_billing_custom_city', esc_attr($_POST['billing_custom_city']), $customer_meta['billing_custom_city'][0] );

        update_post_meta( $order->ID, '_billing_custom_location', esc_attr($_POST['billing_custom_location']), $customer_meta['billing_custom_location'][0] );
     
    }


}

using above hook i cant able to update the value kindly review my code and support me.

Change History (2)

#1 @carike
6 months ago

  • Resolution set to invalid
  • Status changed from new to closed

Hallo! Welcome to Core Trac.

Trac is meant for reporting WordPress Core issues (Core Trac) or issues with the WordPress website (Meta Trac).
The good news is that all plugins hosted on WordPress.org have their own support forums, where those communities can help one another.
You can find the one for WooCommerce here: https://wordpress.org/support/plugin/woocommerce/

I am closing this ticket as "invalid" here for this team's workflow purposes.
That does not mean that it isn't a valid question, just that we can't do something about it here :)

#2 @swissspidy
6 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.