1

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

I used a plugin "WooCommerce Filter Orders by Product" and modified their search by ID, added a get_post_meta(), and now have the ability to filter products by ID.

Plugin: https://en-ca.wordpress.org/plugins/woocommerce-filter-orders-by-product/

Modified code:

    // Display dropdown
    function foa_product_filter_in_order(){
        global $typenow, $wpdb;

        if ( 'shop_order' != $typenow ) {
            return;
        }

        $sql="SELECT ID,post_title FROM $wpdb->posts WHERE post_type = 'product' AND post_status = 'publish'";
        $all_posts = $wpdb->get_results($sql, ARRAY_A);

        $values = array();

        foreach ($all_posts as $all_post) {
            $values[$all_post['ID']] = get_post_meta($all_post['ID'], '_sku', true);
        }



        ?>
        <select name="foa_order_product_filter">
        <option value=""><?php _e('All products', 'foa'); ?></option>
        <?php
            $current_v = isset($_GET['foa_order_product_filter'])? $_GET['foa_order_product_filter']:'';
            foreach ($values as $label) {
                printf
                    (
                        '<option value="%s"%s>%s</option>',
                        $value,
                        $value == $current_v? ' selected="selected"':'',
                        $label
                    );
                }
        ?>
        </select>
        <?php
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.