I have written the code with many update_post_meta entries:
update_post_meta($order_id, '_shipping_first_name', $order->get_billing_first_name() ); update_post_meta($order_id, '_shipping_last_name', $order->get_billing_last_name() ); update_post_meta($order_id, '_shipping_company', $order->get_billing_company() ); update_post_meta($order_id, '_shipping_address_1', $order->get_billing_address_1() ); update_post_meta($order_id, '_shipping_address_2', $order->get_billing_address_2() ); update_post_meta($order_id, '_shipping_postcode', $order->get_billing_postcode() ); update_post_meta($order_id, '_shipping_city', $order->get_billing_city() ); update_post_meta($order_id, '_shipping_state', $order->get_billing_state() ); update_post_meta($order_id, '_shipping_country', $order->get_billing_country() );
For DRY principle I tried to use an array with for loops, but it doesn’t work:
$data = array(
array( '_shipping_first_name', $order->get_billing_first_name() ),
array( '_shipping_last_name', $order->get_billing_last_name() ),
array( '_shipping_company', $order->get_billing_company() ),
array( '_shipping_address_1', $order->get_billing_address_1() ),
array( '_shipping_address_2', $order->get_billing_address_2() ),
array( '_shipping_postcode', $order->get_billing_postcode() ),
array( '_shipping_city', $order->get_billing_city() ),
array( '_shipping_state', $order->get_billing_state() ),
array( '_shipping_country', $order->get_billing_country() )
);
for ( $row = 0; $row < 9; $row++) {
for ( $col = 0; $col < 2; $col++) {
update_post_meta( $order_id, [$row], [$col] );
}
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
To achieve what you want you’re better creating an associative array and then walking over it with a foreach loop as shown below :
$data = array(
'_shipping_first_name' => $order->get_billing_first_name(),
'_shipping_last_name' => $order->get_billing_last_name(),
'_shipping_company' => $order->get_billing_company(),
'_shipping_address_1' => $order->get_billing_address_1(),
'_shipping_address_2' => $order->get_billing_address_2(),
'_shipping_postcode' => $order->get_billing_postcode(),
'_shipping_city' => $order->get_billing_city(),
'_shipping_state' => $order->get_billing_state(),
'_shipping_country' => $order->get_billing_country()
);
foreach ( $data as $meta_key => $meta_value ) {
update_post_meta( $order_id, $meta_key, $meta_value );
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0