在WooCommerce管理订单列表上添加带有元数据的自定义列 [英] Add custom column with metadata on WooCommerce admin orders list

查看:7
本文介绍了在WooCommerce管理订单列表上添加带有元数据的自定义列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在WooCommerce管理订单列表上添加带有一些元数据的自定义列。我找到了它,并根据需要对其进行了修改,并将其放入我的函数。php:

add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
function MY_COLUMNS_FUNCTION( $columns ) {
    $new_columns = ( is_array( $columns ) ) ? $columns : array();
    unset( $new_columns[ 'order_actions' ] );
    
    //edit this for your column(s)
    //all of your columns will be added before the actions column
    $new_columns['dname'] = 'Dogs Name';
    $new_columns['additional_allergies'] = 'Allergies';
    
    //stop editing
    $new_columns[ 'order_actions' ] = $columns[ 'order_actions' ];
    return $new_columns;
}

在结账时,我收集以下两个元密钥:

  • 域名
  • 其他过敏症

但是,我当前的代码只显示空列,有什么建议添加元日期吗?

推荐答案

manage_shop_order_posts_custom_column挂钩可用于添加标头。

// Add a Header
function custom_shop_order_column( $columns ) {
    // Add new columns
    $columns['dogs_name'] = __( 'Dogs Name', 'woocommerce' );
    $columns['additional_allergies'] = __( 'Allergies', 'woocommerce' );
    
    return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 10, 1 );

需要manage_shop_order_posts_custom_column挂钩来填充该列。

  • 注意:重要的是要确定它是属于$order对象的元数据还是属于$items订单的元数据,根据这一点,您必须使用下面两个答案中的一个。

1a:如果元数据属于$order对象,可以使用:

//  Populate the Column
function custom_shop_order_list_column_content( $column, $post_id ) {
    // Get order object
    $order = wc_get_order( $post_id );

    // Is a WC_Order
    if ( is_a( $order, 'WC_Order' ) ) {
        // Compare column name
        if ( $column == 'dogs_name' ) {
            // Get meta, use the correct meta key!
            $dogs_name = $order->get_meta( 'dname' );
            
            // NOT empty
            if ( ! empty( $dogs_name ) ) {
                // Output
                echo $dogs_name;
            } else {
                // Output
                echo __( 'Meta key is wrong or not found for this order', 'woocommerce' );
            }
        }

        // Compare column name
        if ( $column == 'additional_allergies' ) {
            // Get meta, use the correct meta key!
            $allergies = $order->get_meta( 'additional_allergies' );
            
            // NOT empty
            if ( ! empty( $allergies ) ) {
                // Output
                echo $allergies;
            } else {
                // Output
                echo __( 'Meta key is wrong or not found for this order', 'woocommerce' );
            }       
        }
    }
}
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_list_column_content', 10, 2 );

1b:但是,当元数据属于$items顺序时,则需要在上面的答案中进行调整,因为一个$order可以由几个$items组成,我们将使用foreach

循环$order对象
//  Populate the Column
function custom_shop_order_list_column_content( $column, $post_id ) {
    // Get order object
    $order = wc_get_order( $post_id );

    // Is a WC_Order
    if ( is_a( $order, 'WC_Order' ) ) {
        // Initialize
        $dogs_name_arr = array();
        $allergies_arr = array();
        
        // Loop trough order items
        foreach ( $order->get_items() as $item_key => $item ) {
            // Get meta, use the correct meta key!
            $dogs_name = $item->get_meta( 'dname' );
            
            // NOT empty
            if ( ! empty ( $dogs_name ) ) {
                // Push to array
                $dogs_name_arr[] = $dogs_name;
            }
            
            // Get meta, use the correct meta key!
            $allergies = $item->get_meta( 'additional_allergies' );
            
            // NOT empty
            if ( ! empty ( $allergies ) ) {
                // Push to array
                $allergies_arr[] = $allergies;
            }
        }
        
        // Compare column name
        if ( $column == 'dogs_name' ) {
            // NOT empty
            if ( ! empty ( $dogs_name_arr ) ) {
                // Output
                echo '<ul>';
                echo '<li>' . implode( '</li><li>', $dogs_name_arr ) . '</li>';
                echo '</ul>';
            } else {
                // Output
                echo __( 'Meta key is wrong or not found for the order items', 'woocommerce' );
            }
        }

        // Compare column name
        if ( $column == 'additional_allergies' ) {
            // NOT empty
            if ( ! empty ( $allergies_arr ) ) {
                // Output
                echo '<ul>';
                echo '<li>' . implode( '</li><li>', $allergies_arr ) . '</li>';
                echo '</ul>';
            } else {
                // Output
                echo __( 'Meta key is wrong or not found for the order items', 'woocommerce' );
            }
        }
    }
}
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_list_column_content', 10, 2 );

这篇关于在WooCommerce管理订单列表上添加带有元数据的自定义列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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