wordpress - 在 WooCommerce "My account"订单表的新列中显示产品类别

标签 wordpress woocommerce product orders taxonomy-terms

我想添加一个自定义列,以在 wooCommerce 的订单历史表中显示产品类别

我找到了如何添加自定义列,但我似乎无法在此列中显示链接到订单的分类产品。

对于这个例子,我只有 1 种产品,但如果我能显示不止一种税,那就更好了。

这是我发现的(来自:skyverge 博客)添加新专栏:

/**
 * 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' );

欢迎指点

最佳答案

使用您当前的代码,您可以在现有列之间添加一列,但是:

  • woocommerce_my_account_my_orders_columns 过滤器自 WooCommerce 2.6.0 起已弃用。并替换为 woocommerce_account_orders_columns
  • 在列中添加内容的部分丢失

要添加内容,您可以使用 woocommerce_my_account_my_orders_column_{$column_id} Hook , 在这种特殊情况下,$column_id 需要替换为 order-category

所以你得到:

// Adds a new column to the "My Orders" table in the account.
function filter_woocommerce_account_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_account_orders_columns', 'filter_woocommerce_account_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 );

关于wordpress - 在 WooCommerce "My account"订单表的新列中显示产品类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69649960/

相关文章:

woocommerce - 添加自定义字段 woocommerce(可变产品)

api - 是否有适用于亚马逊供应商中心的 API?

html - 在 WordPress 中更改表格的列

php - 获取 WooCommerce 属性值 (url) 以将其用作 href 链接

php - Wordpress - 将数据写入 SQL Server

Magento 不会在主页上显示所有产品

wordpress - 如何以编程方式将文章发布到 WordPress?

javascript - 当用户向下滚动我的网页 100 像素左右时淡出图像(向下箭头)

php - 在 WooCommerce 中的标题下方显示循环产品类别项目描述

php - 如何在购物车页面 "WooCommerce"中显示航运等级的零费率值