3
votes

I am trying to extract order meta data of a custom field that I added to my WooCommerce checkout form. I am doing this in a PHP file that is used to add a custom section to the order confirmation email sent to the customer by WooCommerce (i.e. the very last stage of order processing).

In order to do this, I am trying to use WC_Order class with following code:

$my_var = get_post_meta( $order_id, 'custom_field_meta_key', true );

However, this approach doesn't work. I realised that the "order_id" variable is not available in the PHP page. I used the following code to confirm this:

print_r(array_keys(get_defined_vars()));

Below is the output that I get:

Array ( [0] => template_name [1] => args [2] => template_path [3] => default_path [4] => cache_key [5] => template [6] => filter_template [7] => action_args [8] => title [9] => delivery_date [10] => email )

Can somebody please help me with the best way to get the expected outcome?

1

1 Answers

4
votes

As $email variable seems to be defined, you can normally get the WC_Order Object instance using:

$order = $email->object;

Then to get your custom field (meta data) you can use the WC_Data method get_meta() like:

$my_var = $order->get_meta( 'custom_field_meta_key' );

or you can also use the old WordPress way (from the order Id):

$my_var = get_post_meta( $order->get_id(), 'custom_field_meta_key', true );