Halloween Sale 15% off Ends in See the plans
PHP

Add Custom Order Fields to WooCommerce Privacy Exports

Includes extra checkout fields you store on orders (such as a VAT number or delivery notes) in the order section of personal data exports.

Don’t have WPIDE yet? Install it on your site

Code

// Order meta key => label in the export.
$wpide_woo_export_meta = array(
    '_billing_vat_number' => 'VAT number',
    '_delivery_notes'     => 'Delivery notes',
);

add_filter( 'woocommerce_privacy_export_order_personal_data_props', function ( $props ) use ( $wpide_woo_export_meta ) {
    foreach ( $wpide_woo_export_meta as $key => $label ) {
        $props[ 'wpide_meta_' . $key ] = $label;
    }

    return $props;
} );

add_filter( 'woocommerce_privacy_export_order_personal_data_prop', function ( $value, $prop, $order ) use ( $wpide_woo_export_meta ) {
    if ( 0 !== strpos( $prop, 'wpide_meta_' ) || ! is_a( $order, 'WC_Order' ) ) {
        return $value;
    }

    $key = substr( $prop, strlen( 'wpide_meta_' ) );

    return isset( $wpide_woo_export_meta[ $key ] ) ? (string) $order->get_meta( $key ) : $value;
}, 10, 3 );

Notes

Use the order meta keys your checkout field plugin saves; keys starting with an underscore are fine.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.