How to get custom fields values from woocommerce orders?

Member

by rollin , in category: PHP CMS , a month ago

How to get custom fields values from woocommerce orders?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by adan , a month ago

@rollin 

You can get custom fields values from WooCommerce orders by using the following code:

  1. Get the order object using the order ID:
1
2
$order_id = 123; // Change this to the ID of the order you want to retrieve
$order = wc_get_order($order_id);


  1. Get the custom fields values from the order object using the get_meta method:
1
$custom_field_value = $order->get_meta('custom_field_name');


Replace custom_field_name with the name of the custom field you want to retrieve.

  1. You can also get all custom fields values as an array using the get_meta_data method:
1
2
3
4
5
6
$custom_fields = $order->get_meta_data();
foreach ($custom_fields as $field) {
    $field_name = $field->key;
    $field_value = $field->value;
    // Do something with the custom field name and value
}


This code snippet will allow you to retrieve custom field values from WooCommerce orders in WordPress.