Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter th3k1d

    (@th3k1d)

    Thanks for your reply Phillip!

    In the end I created a child theme and added some code to functions.php by browsing stackoverflow and adapting some solutions on there to my needs.
    I’ll upload the code so anyone that needs it can check it out as well:

    add_filter( 'manage_edit-shop_order_columns', 'shop_order_columns' );
    function shop_order_columns( $columns ){
        $new_columns = (is_array($columns)) ? $columns : array();
    
        $new_columns['category'] = 'Categorie';
    
        return $new_columns;
    }
    
    add_action( 'manage_shop_order_posts_custom_column', 'shop_order_posts_custom_column' );
    function shop_order_posts_custom_column( $column ){
        global $post, $the_order;
    
        if ( empty( $the_order ) || $the_order->get_id() != $post->ID ) {
            $the_order = wc_get_order( $post->ID );
        }
    
        $category_name_array = array();
        $i=0;
    
        foreach ($the_order->get_items() as $item_id => $item_data) {
            $product = $item_data->get_product();
            $category = $product->get_category_ids();
    
            $name = get_product_category_by_id($category[0]);
            $category_name_array[$i] = $name;
            $i++;
        }
    
        if ( $column == 'category' ) {    
            $arrlength = count($category_name_array);
            $category_to_display = '';
            for($x = 0; $x < $arrlength; $x++){
                if($x == 0){
                    $category_to_display = $category_name_array[$x];
                } else{
                    $category_to_display = $category_to_display.", ".$category_name_array[$x];
                }
            }
            echo($category_to_display);
        }
    }
    
    function get_product_category_by_id( $category_id ) {
        $term = get_term_by( 'id', $category_id, 'product_cat', 'ARRAY_A' );
        return $term['name'];
    }

    this creates an extra column in my Orders page named “Categorie” and adds the category of every product in an order!

    • This reply was modified 7 years, 3 months ago by th3k1d.
    Thread Starter th3k1d

    (@th3k1d)

    Hi Philip,

    the plugin you linked does look like something that would help, but I think it’s a bit overkill for what I’m trying to do!

    Is there a way to modify the orders page? I’m not below editing some code to add what I need!

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)