How to get custom fields values from woocommerce orders?

Member

by rollin , in category: PHP CMS , 10 months ago

How to get custom fields values from woocommerce orders?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by adan , 10 months 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.

Related Threads:

How to get orders by user meta data in woocommerce?
How to get woocommerce orders total sales without taxes?
How to add custom registration fields to woocommerce?
How to add custom fields addresses to woocommerce product?
How to add label for custom fields in woocommerce?
How to get total sum of orders by a user in woocommerce?