elimariaaa
elimariaaa

Reputation: 866

wc_update_order() Not Working as expected

I am trying to update the customers' billing details in frontend. I have the following code:

if(isset($_POST['save_order'])){
    $update_billing_details = wc_update_order( array('order_id' => $update_order_id)); 
    $update_order_args = array(
        'first_name' => $_POST['billing_first_name']
    );
$update_billing_details->set_address( $update_order_args, 'billing' );
 if($update_billing_details){
    echo "success";
 }
}

What happens is, The first name is being updated after hitting the save button TWICE.

Example:

The original name is 'John'. If I make it 'Johnny' and hit save, it still shows 'John'. And if I type the name 'Johndel' then hit save, it became 'Johnny', and so on.

But, if I make my code like this:

if(isset($_POST['save_order'])){
  $update_order_args = array(
                '_billing_first_name' => $_POST['billing_first_name'],
                'order_id' => $update_order_id
                );
  $update_billing_details = wc_update_order( $update_order_args ); 
}

Nothing's happening.

What am I doing wrong? I based my work through this question.

Any help is highly appreciated.

Thanks,

-Eli

Upvotes: 1

Views: 985

Answers (2)

Pablo S G Pacheco
Pablo S G Pacheco

Reputation: 2600

Actually, wc_update_order() uses wc_create_order() function, so you can only update these parameters for now:

$args = array(
    'status'        => null,
    'customer_id'   => null,
    'customer_note' => null,
    'parent'        => null,
    'created_via'   => null,
    'cart_hash'     => null,
    'order_id'      => 0,
);

Upvotes: 1

LoicTheAztec
LoicTheAztec

Reputation: 253784

You could try to use instead update_post_meta() function, this way:

if(isset($_POST['save_order']) && isset($_POST['billing_first_name'])){
    update_post_meta( $update_order_id, '_billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
}

You have to be sure that $update_order_id is the defined Order ID here.

As I can't test this I can't guaranty anything… I hope this will work.

Upvotes: 1

Related Questions