Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search by order item SKU or ID in WooCommerce Orders Admin page

What I am trying to do is to be able to search by order item SKU or ID in the WooCommerce Orders Admin page.

What I have found/done till now, but with no success is the following at functions.php file.

add_filter( 'woocommerce_shop_order_search_fields', 'woocommerce_shop_order_search_sku' );

function woocommerce_shop_order_search_sku( $search_fields ) {

    $args = array( 'post_type' => 'shop_order' );

    $orders = new WP_Query( $args );

    if ( $orders->have_posts() ) {
        while( $orders->have_posts() ) {
            $post = $orders->the_post();
            $order_id = get_the_ID();
            $order = new WC_Order( $order_id );
            $items = $order->get_items();
            foreach( $items as $item ) {
                $search_order_item_sku = wp_get_post_terms( $item['product_id'], 'search_sku' );
                foreach( $search_order_item_sku as $search_sku ) {
                    add_post_meta( $order_id, "_search_sku", $search_sku->sku );
                }
            }
        }
    };

    $search_fields[] = '_search_sku';

    return $search_fields;

}

I suppose the issue is the value of $search_sku at the line with the add_post_meta.

I have also tried it with get_sku(), $item['sku'] with no luck.

like image 647
Nikos Avatar asked Jan 21 '15 09:01

Nikos


People also ask

How do I find order details by order ID in WooCommerce?

You have access to $order_id variable If you have access to the order ID (once again, usually the do_action or apply_filters might give you this), you have to get the order object first. Then do the exact same things as above. $order = wc_get_order( $order_id ); // Now you have access to (see above)...

What is the order ID in WordPress?

The product in WooCommerce is simply a custom post type, just like any other custom post type in WordPress. These items and orders, like any other WordPress post type, are given page IDs. The order ID is a one-of-a-kind number issued to each order when made for identification and usage in other WooCommerce services.

Where are WooCommerce orders stored in the database?

Woocommerce Orders are stored in the database as custom posts. The custom post type is named shop_order. The WordPress post ID maps to the order ID and the post status describes the status of the order (ex: wc-pending).


2 Answers

You have the right idea about saving extra metadata to the order. As jbby and helgatheviking suggest, there is no built-in postmeta for product_id or sku available by default in the woocommerce orders api. Your methodology for accessing and saving the metadata wasn't quite right, however. wp_get_post_terms will access custom taxonomy information, not metadata (use get_post_meta for that). You will be able to do what you were trying to do with this filter:

add_filter( 'woocommerce_shop_order_search_fields', function ($search_fields ) {
    $posts = get_posts(array('post_type' => 'shop_order'));

    foreach ($posts as $post) {
        $order_id = $post->ID;
        $order = new WC_Order($order_id);
        $items = $order->get_items();

        foreach($items as $item) {
            $product_id = $item['product_id'];
            $search_sku = get_post_meta($product_id, "_sku", true);
            add_post_meta($order_id, "_product_sku", $search_sku);
            add_post_meta($order_id, "_product_id", $product_id);
        }
    }

    return array_merge($search_fields, array('_product_sku', '_product_id'));
});

Strictly speaking you should probably move the calls to add_post_meta into a hook that runs when the order is originally saved to the database--this will prevent unnecessary legwork whenever you search through order.

like image 99
kellanburket Avatar answered Nov 03 '22 01:11

kellanburket


@blacksquare, @jibby, @helgatheviking you are the men! This is the code that works, due to your help.

    //Search by product SKU in Admin Woocommerce Orders
add_filter( 'woocommerce_shop_order_search_fields', function ($search_fields ) {
    $posts = get_posts(array('post_type' => 'shop_order'));

    foreach ($posts as $post) {
        $order_id = $post->ID;
        $order = new WC_Order($order_id);
        $items = $order->get_items();

        foreach($items as $item) {
            $product_id = $item['product_id'];
            $search_sku = get_post_meta($product_id, "_sku", true);
            add_post_meta($order_id, "_product_sku", $search_sku);
        }
    }

    return array_merge($search_fields, array('_product_sku'));
});
like image 21
Nikos Avatar answered Nov 02 '22 23:11

Nikos