Last active
October 26, 2022 12:17
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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