Skip to content

Instantly share code, notes, and snippets.

@marcusig
Last active October 26, 2022 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcusig/f660b9f85aa8074d52ed39f38494f941 to your computer and use it in GitHub Desktop.
Save marcusig/f660b9f85aa8074d52ed39f38494f941 to your computer and use it in GitHub Desktop.
Filter the rest api result when exporting orders, replacing the meta value by display_value, which stores the readable data.
<?php
add_filter( "woocommerce_rest_prepare_shop_order_object", function( $response, $object, $request ) {
foreach( $response->data['line_items'] as $ind => $item ) {
foreach( $item['meta_data'] as $md_ind => $meta_data ) {
// If the value contains order-configuration-details
if ( strpos( $meta_data['value'], 'order-configuration-details' ) ) {
// then replace the value by display_value
$response->data['line_items'][$ind]['meta_data'][$md_ind]['value'] = $meta_data['display_value'];
}
}
}
return $response;
}, 20, 3 );
add_filter( 'woocommerce_api_order_response', function( $order_data, $order, $fields, $server ) {
foreach( $order_data['line_items'] as $index => $item ) {
$wc_order_item = $order->get_item( $item['id'] );
if ( ! $wc_order_item || ! function_exists( 'mkl_pc' ) ) continue;
mkl_pc()->frontend->order->maybe_override_formatted_meta_data( $item['meta'], $wc_order_item );
foreach( $item['meta'] as $meta_index => $meta ) {
if ( $meta->value && strpos( $meta->value, 'order-configuration-details' ) && property_exists( $meta, 'display_value' ) ) {
$meta->value = $meta->display_value;
}
}
}
return $order_data;
}, 20, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment