Home > Enterprise >  Show product categories in a new column on WooCommerce "My account" orders table
Show product categories in a new column on WooCommerce "My account" orders table

Time:10-21

I want to add a custom column, to display the product categories on the orders history table in wooCommerce

I found how to add a custom column but I can't seem to display the taxonomy's product linked to the order in this column.

For this example, I had just 1 product, but if I can display more than one tax, it will be better.

This is what I found (from : skyverge blog) to add a new column:

/**
 * Adds a new column to the "My Orders" table in the account.
 *
 * @param string[] $columns the columns in the orders table
 * @return string[] updated columns
 */
function sv_wc_add_my_account_orders_column( $columns ) {

    $new_columns = array();

    foreach ( $columns as $key => $name ) {

        $new_columns[ $key ] = $name;

        // add ship-to after order status column
        if ( 'order-number' === $key ) {
            $new_columns['order-ship-to'] = __( 'Catégorie', 'textdomain' );
        }
    }

    return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'sv_wc_add_my_account_orders_column' );

Any pointers are welcome

CodePudding user response:

With your current code you can add a column between the existing columns, however the part to add content in the column is missing

For that you can use the woocommerce_my_account_my_orders_column_{$column_id} hook, where $column_id need to be replaced by order-category in this particular case

So you get:

// Adds a new column to the "My Orders" table in the account.
function filter_woocommerce_my_account_my_orders_columns( $columns ) {
    $new_columns = array();

    foreach ( $columns as $key => $column ) {

        $new_columns[ $key ] = $column;

        // Add after order number column
        if ( $key === 'order-number' ) {
            $new_columns['order-category'] = __( 'Catégorie', 'woocommerce' );
        }
    }

    return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'filter_woocommerce_my_account_my_orders_columns', 10, 1 );

// Adds data to the custom "order-category" column in "My Account > Orders"
function filter_woocommerce_my_account_my_orders_column_order( $order ) {
    // Initialize
    $categories = array();
    
    // Loop through order items
    foreach ( $order->get_items() as $item_key => $item ) {
        // Get product ID
        $product_id = $item->get_product_id();
        
        // Get terms
        $term_names = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'names' ) );
        
        // Loop through term names
        foreach ( $term_names as $term_name ) { 
            // NOT in array
            if ( ! in_array( $term_name, $categories, true ) ) {
                // Push one or more elements onto the end of array
                array_push( $categories, $term_name );
            }
        }
    }
    
    // NOT empty
    if ( ! empty( $categories ) ) {
        echo implode( ', ', $categories );
    }
}
add_action( 'woocommerce_my_account_my_orders_column_order-category', 'filter_woocommerce_my_account_my_orders_column_order', 10, 1 );
  • Related