Admin filter for product SKU?

Question

How can I filter products based on their SKU code?

A new column was created in the orders, but I’m having a hard time understanding how I can search for it.

I have a column populated with product SKU’s. How can I return search results based on that info?

    // Add Column
    add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column',11);
    function custom_shop_order_column($columns)
    {
        //add columns
        $columns['SKU'] = __( 'SKU');
        return $columns;
    }

    // Add Column Data
    add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 10, 2 );
    function custom_orders_list_column_content( $column, $post_id )
    {
        switch ( $column )
        {
            case 'SKU' :
                $order = wc_get_order( $post_id );
                $order_item = $order->get_items();

                foreach ( $order_item as $item_id ) {

                    $product_id = $item_id['product_id']; 

                    $myVarOne = get_post_meta($product_id, '_sku', true);

                    echo $myVarOne;
                    break;
        }
    }
    }

    // Failed search functionality
    add_filter( 'woocommerce_shop_order_search_fields', 'woocommerce_shop_order_search_SKU' );
    function woocommerce_shop_order_search_SKU( $search_fields ) {
        $search_fields[] = '_sku';
        return $search_fields;
    }
0
Kiril Climson 5 years 2017-12-07T19:54:34-05:00 0 Answers 70 views 0

Leave an answer

Browse
Browse