Show products name and quantity in a new column on Woocommerce My account Orders

蹲街弑〆低调 提交于 2021-02-04 06:36:25

问题


In Woocommerce, I would like to add a new column to My account orders list and show the name and quantity of ordered the products.

I have this code for instance that adds a new column displaying the order name:

function sv_wc_add_my_account_orders_column( $columns ) {
    $new_columns = array();

    foreach ( $columns as $key => $name ) {
        $new_columns[ $key ] = $name;

        if ( 'order-status' === $key ) {
            $new_columns['order-ship-to'] = __( 'Ship to', 'textdomain' );
        }
    }
    return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'sv_wc_add_my_account_orders_column' );

function sv_wc_my_orders_ship_to_column( $order ) {
    $formatted_shipping = $order->get_name;

    echo ! empty( $formatted_shipping ) ? $formatted_shipping : '–';
}
add_action( 'woocommerce_my_account_my_orders_column_order-ship-to', 'sv_wc_my_orders_ship_to_column' );

How can I change it to display the name and quantity of ordered products?


回答1:


The following code will add a custom column to My account orders list section, displaying the product name and quantity for each order:

add_filter( 'woocommerce_my_account_my_orders_columns', 'additional_my_account_orders_column', 10, 1 );
function additional_my_account_orders_column( $columns ) {
    $new_columns = [];

    foreach ( $columns as $key => $name ) {
        $new_columns[ $key ] = $name;

        if ( 'order-status' === $key ) {
            $new_columns['order-items'] = __( 'Product | qty', 'woocommerce' );
        }
    }
    return $new_columns;
}

add_action( 'woocommerce_my_account_my_orders_column_order-items', 'additional_my_account_orders_column_content', 10, 1 );
function additional_my_account_orders_column_content( $order ) {
    $details = array();

    foreach( $order->get_items() as $item )
        $details[] = $item->get_name() . ' × ' . $item->get_quantity();

    echo count( $details ) > 0 ? implode( '<br>', $details ) : '&ndash;';
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.



来源:https://stackoverflow.com/questions/55550835/show-products-name-and-quantity-in-a-new-column-on-woocommerce-my-account-orders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!