wordpress - 在 WooCommerce 管理订单列表上添加多个自定义列(备注和增值税)

标签 wordpress woocommerce backend checkout orders

我已经插入了此代码片段,以便在订单中有客户备注时显示带有图标的附加列。它有效。

add_action( 'wp_enqueue_scripts', 'mini_enqueue_scripts' );

add_filter( 'manage_shop_order_posts_columns', 'woocommerce_add_order_notes_column', 99 );
function woocommerce_add_order_notes_column( $columns ) {
    $columns['order_notes'] = __('Customer note', 'woocommerce');
    return $columns;
}

add_action( 'manage_shop_order_posts_custom_column' , 'woocommerce_show_order_notes_column', 10, 2 );
function woocommerce_show_order_notes_column( $column_name, $order_id ) {
    switch ( $column_name ) {
        case 'order_notes':
            $order = wc_get_order( $order_id );
            $note = $order->get_customer_note();
            if ( !empty($note) ) {
                echo '<span class="note-on tips" data-tip="' . wc_sanitize_tooltip( $note ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
            } else {
                echo '<span class="na">&ndash;</span>';
            }
            break;
    }
}

如何在执行相同操作的一侧创建另一列(如果已在订单中输入增值税号)?

为此,我在结账页面上有两个附加字段:“增值税号”(billing_piva) 和“CID 号”(billing_cid)。我希望将它们放在同一列上,首先有两个标题,例如:

  • “增值税号:xxx”和“CID 号:xxx”

有什么建议吗?

最佳答案

要添加 2 列而不是 1 列,您实际上可以应用与添加 1 列相同的方法

可以通过不同的方式将增值税号添加到订单中,从您的问题来看,我了解到这是一个带有元键的自定义结帐字段:billing_piva

注意:使用 add_action( 'wp_enqueue_scripts', 'mini_enqueue_scripts' ); 不是必需的

所以你得到:

// Display on order admin list (header)
function filter_manage_edit_shop_order_columns( $columns ) {
    // Add columns
    $columns['order_notes'] = __( 'Customer note', 'woocommerce' );
    $columns['order_vat'] = __( 'VAT number', 'woocommerce' );

    return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );

// Display on order admin list (populate the column)
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {
    // Get order
    $order = wc_get_order( $post_id );
    
    // Is a WC_Order
    if ( is_a( $order, 'WC_Order' ) ) {
        // Compare
        switch ( $column ) {
            case 'order_notes':
                // Get customer note
                $note = $order->get_customer_note();
                
                // NOT empty
                if ( ! empty( $note ) ) {
                    echo '<span class="note-on tips" data-tip="' . wc_sanitize_tooltip( $note ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
                } else {
                    echo '<span class="na">&ndash;</span>';
                }
                
                break;
            case 'order_vat':
                // Get VAT (if necessary, adjust to the correct meta key)
                $vat_number = $order->get_meta( 'billing_piva' );
                
                // NOT empty
                if ( ! empty( $vat_number ) ) {
                    // Output
                    $output = '<span>' . sprintf( __( 'VAT Number: %s', 'woocommerce' ), $vat_number ) . '</span>';
                    
                    // Get CID number
                    $cid_number = $order->get_meta( 'billing_cid' );
                    
                    // NOT empty 
                    if ( ! empty ( $cid_number ) ) {
                        // Concatenation 
                        $output .= '<br><span>' . sprintf( __( 'CID Number: %s', 'woocommerce' ), $cid_number ) . '</span>';
                    }
                    
                    // Print
                    echo $output;
                } else {
                    echo '<span class="na">&ndash;</span>';
                }               
        }
    }
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );

关于wordpress - 在 WooCommerce 管理订单列表上添加多个自定义列(备注和增值税),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69663782/

相关文章:

css - 联系表格 7 "Send"按钮丢失

wordpress - 错误 Woocommerce REST API 扩展订单 "line_items"响应

php - Laravel 验证 : Check Exist in two tables

mysql - Laravel 和 Backendless

javascript - Azure Javascript - 等待函数完成

javascript - 主 Web 文档与 iframe 脚本

php - Wordpress:访问存储在主题文件夹中的子文件夹中的模板

php - 如何更改 "post comment"按钮上的文本?

php - 按作者性别查询帖子

Woocommerce 获得航运区