Skip to content
Advertisement

Search value of custom coulmn in Woocommerce Admin Orders list

I got a help from people here to create a coulmn in the woocommerce>orders table to preview the description of the first product in the order only. by the code below:

Now how can i make it searchable when i search by the value of the column to show the orders that contain that product description only

All i need is to make this column values to be searchable as they don’t now.

/**
 * Adds 'Description' column header to 'Orders' page immediately after 'Order ID' column.
 *
 * @param string[] $columns
 * @return string[] $new_columns
 */
function sv_wc_cogs_add_order_description_column_header( $columns ) {
    $new_columns = array();
    foreach ( $columns as $column_name => $column_info ) {
        $new_columns[ $column_name ] = $column_info;
        if ( 'order_number' === $column_name ) {
            $new_columns['order_description'] = __( 'Description', 'my-textdomain' );
        }
    }
    return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'sv_wc_cogs_add_order_description_column_header', 99 );

/**
 * Adds 'Description' column content to 'Orders' page immediately after 'Order ID' column.
 *
 * @param string[] $column name of column being displayed
 */
function sv_wc_cogs_add_order_description_column_content( $column ) {
    global $post;
    if ( 'order_description' === $column ) {
        $order    = wc_get_order( $post->ID );
        if( $order ) {
           $first_line_item_descrp = '';
           foreach ( $order->get_items( 'line_item' ) as $item_id => $item ) {
               $product      = $item->get_product();
               $first_line_item_descrp = $product->get_description();
               break;
           }
           echo $first_line_item_descrp;
        }
    }
}
add_action( 'manage_shop_order_posts_custom_column', 'sv_wc_cogs_add_order_description_column_content', 99 );

I want now to be able to search by “success event” for example that is in “event” column that is generated by the product description of the first item in the order is it possible ?

Screenshot: enter image description here

Advertisement

Answer

Please check the below code.

Note: When the Plugin update this code will automatically remove.

Solution:- File Path:/plugins/woocommerce/includes/data-stores/class-wc-order-data-store-cpt.php

Function name :- search_orders()

After this code

if (is_numeric($term)) {$order_ids[] = absint($term);}

Add this code

$data_store = WC_Data_Store::load('product');
$product_ids = $data_store->search_products($term, ' ', true, false, '');

$custome_order_ids = $wpdb->get_col(
            $wpdb->prepare(
                "SELECT order_items2.order_id
                            FROM {$wpdb->prefix}woocommerce_order_itemmeta as order_itemmeta
                            JOIN {$wpdb->prefix}woocommerce_order_items as order_items2 ON order_items2.order_item_id = order_itemmeta.order_item_id
                            WHERE order_itemmeta.meta_value IN ('" . implode("','", $product_ids) . "') AND order_itemmeta.meta_key = %s", '_product_id'
            )
        );
$order_ids = array_merge($order_ids, $custome_order_ids);

Note: When Plugin update this code will remove.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement