I’m trying to update the data relating to a woocommerce order. I can retrieve the existing data but not update.
Before I start, I’ve manually created an order by going to the orders page of the Dashboard, clicking the Add Order button then filling in Shipping and Billing address details and adding some products.
Then I create a copy of woocommerce/checkout/form-pay.php in my theme, to which I add the code:
$invoiceid = $order->id;
echo invoiceid;
This returns the id number that corresponds with that in the URL, so I know I’m in the right place. I next try drilling into the address details relating to the order. To keep things simple until I have a working process, I’ll just update the ‘Billing First Name’ field. I enter:
$billing_first_name = $order->get_billing_first_name();
echo $billing_first_name;
Again, it returns the value which I entered in that field when I manually created the order. So far, so good. However, things fall apart when I try to update the value.
After reading this WooCommerce page on CRUD, I tried:
$order->set_billing_first_name( 'Foo' );
$order->save;
But the value doesn’t update. Based on this page, I have also tried:
$order->update_meta_data( billing_first_name, 'foo' );
$order->save;
This didn’t result in the field being updated either.
What is the correct process/syntax for updating this field?
2
Answers
You can update order meta with order id. Here is snippet you may change hook as per your requirement. Need to place this code in functions.php.
You should use:
$order->set_billing_first_name( 'Foo' ); $order->save();
instead of:
$order->set_billing_first_name( 'Foo' ); $order->save;