Search code examples
phpwordpresswoocommercebackendorders

Remove duplicate values from custom column on WooCommerce admin orders list


I have the following code that adds a custom column on WooCommerce admin orders list

add_action( 'manage_shop_order_posts_custom_column', 'inforder_add_new_order_admin_list_column_content' );
 
function inforder_add_new_order_admin_list_column_content( $column ) {
   
    global $post;
 
if ( 'product_order' === $column ) {

        $order = wc_get_order( $post->ID );
        $items = $order->get_items();
        $count = count($items);
        $i = 1;
        foreach($items as $item) {
            if($i==$count) 
                echo $item->get_name();
            else
                echo $item->get_name() .', ';
            $i++;
        }
    }
    
    
}

But this shows duplicate values. I've tried to avoid this using array_unique(), but unfortunately without the desired result.

Any advice to avoid this?


Solution

  • Your code contains some mistakes

    • The manage_edit-shop_order_columns is missing, which is for adding columns
    • The manage_shop_order_posts_custom_column has not 1 but 2 parameters, the 2nd contains the $post_id so the use of global $post is not necessary
    • You can use in_array(), which checks if a value exists in an array. If this is not the case, we will add this value to the array, this way we avoid duplicate values

    So you get:

    // Add header
    function filter_manage_edit_shop_order_columns( $columns ) {    
        $columns['product_order'] = __( 'Product', 'woocommerce' );
    
        return $columns;
    }
    add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );
    
    // Display (populate the column)
    function action_manage_shop_order_posts_custom_column( $column, $post_id ) {    
        // Compare
        if ( $column == 'product_order' ) {
            // Get order
            $order = wc_get_order( $post_id );
            
            // Initialize
            $names = array();
    
            // Is a WC_Order
            if ( is_a( $order, 'WC_Order' ) ) {
                // Get items
                $items = $order->get_items();
                
                // Loop through
                foreach ( $items as $key => $item ) {
                    // Get name
                    $name = $item->get_name();
                    
                    // NOT in array
                    if ( ! in_array( $name, $names, true ) ) {
                        // Push one or more elements onto the end of array
                        array_push( $names, $name );
                    }
                }
                
                // NOT empty, print result
                if ( ! empty( $names ) ) {
                    // Use implode() function to join comma in the array
                    $list = implode( ', ', $names );
                    
                    // Output
                    echo $list;
                }
            }
        }
    }
    add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );