在 Woocommerce 我的帐户订单的新列中显示产品名称和数量 [英] Show products name and quantity in a new column on Woocommerce My account Orders

查看:74
本文介绍了在 Woocommerce 我的帐户订单的新列中显示产品名称和数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Woocommerce 中,我想在我的帐户订单列表中添加一个新列,并显示所订购产品的名称和数量.

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?

推荐答案

以下代码将在我的帐户订单列表部分添加一个自定义列,显示每个订单的产品名称和数量:

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;';
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

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

这篇关于在 Woocommerce 我的帐户订单的新列中显示产品名称和数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆