Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add WooCommerce Order item data to WooCommerce Orders Search in SearchWP
<?php
// Add WooCommerce Order item data to WooCommerce Orders Search in SearchWP.
// @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-orders/
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) {
// The Product data keys to index for each Order item.
$data_to_index = [
'sku',
'name',
'slug',
'description',
'short_description',
'attributes',
];
if (
'post' . SEARCHWP_SEPARATOR . 'shop_order' !== $entry->get_source()->get_name()
|| ! function_exists( 'wc_get_order' )
|| ! class_exists( 'WC_Order_Item_Product' )
|| ! method_exists( 'WC_Order_Item_Product', 'get_product' )
|| ! class_exists( 'WC_Product' )
|| ! method_exists( 'WC_Product', 'get_data' )
) {
return $data;
}
$order = wc_get_order( $entry->get_id() );
$data['meta']['searchwp_wc_order_item_data'] = array_map( function ( $item ) use ( $data_to_index ) {
$product = $item->get_product();
$product_data = $product->get_data();
return array_filter( array_map( function( $item_key, $item_value ) use ( $data_to_index ) {
return in_array( $item_key, $data_to_index ) ? $item_value : '';
}, array_keys( $product_data ), array_values( $product_data ) ) );
}, $order->get_items() );
return $data;
}, 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.