php - 在 WooCommerce 我的帐户订单列表中添加发送给客户的管理订单备注

标签 php wordpress woocommerce orders account

基于WooCommerce admin orders list custom column with order notes sent to customer答案代码,我尝试添加以下代码以在 WooCommerce 我的帐户订单列表中创建一个新列,但未成功,我已将 manage_edit-shop_order_columns 更改为 woocommerce_my_account_my_orders_columns Hook ,例如:

// Add custom column on admin orders list page
add_filter( 'woocommerce_my_account_my_orders_columns', 'add_order_notes_column' );
function add_order_notes_column( $columns ) {
    $columns['order_notes'] = 'Order Notes';
    return $columns;
}

// CSS styles
add_action( 'admin_print_styles', 'add_order_notes_column_style' );
function add_order_notes_column_style() {
    $css = '.post-type-shop_order table.widefat.fixed { table-layout: auto; width: 100%; }';
    $css .= 'table.wp-list-table .column-order_notes { min-width: 280px; text-align: left; }';
    $css .= '.column-order_notes ul { margin: 0 0 0 18px; list-style-type: disc; }';
    // $css .= '.order_customer_note { color: #ee0000; }'; // red
    // $css .= '.order_private_note { color: #0000ee; }'; // blue
    wp_add_inline_style( 'woocommerce_admin_styles', $css );
}

// Admin orders list custom column displayed content
add_action( 'woocommerce_my_account_my_orders_columns', 'add_order_notes_content' );
function add_order_notes_content( $column ) {
    global $post, $the_order;

    if( 'order_notes' !== $column )
        return;

    $order = is_a($the_order, 'WC_Order') ? $the_order : wc_get_order( $post->ID );

    $notes = wc_get_order_notes( array(
        'order_id' => $order->get_id(),
        'order_by' => 'date_created',
        'order' => 'ASC',
    ) );

    if( ! empty($notes) ) {
        echo '<ul>';

        foreach( $notes as $note ) {
            if( $note->customer_note && 'system' !== $note->added_by ) {
                echo '<li class="order_customer_note">' . sprintf( __('%s by %s <br> %s:'),
                    date_i18n( 'm/d/y H:i', strtotime( $note->date_created ) ),
                    $note->added_by,
                    $note->content
                ) . '</li>';
            }
        }
        echo '</ul>';
    }
}

我希望本栏目内容相同,但我希望标题为“下载”。

更新:这是我希望该列所在的屏幕截图:

screenshot of columns

最佳答案

您的代码中有很多错误...要将管理员在“我的帐户订单”中发送的客户订单注释显示为新列,请改用以下内容:

// Add custom column on admin orders list page
add_filter( 'woocommerce_my_account_my_orders_columns', 'add_myaccount_admin_order_notes_column' );
function add_myaccount_admin_order_notes_column( $columns ) {
    $column_actions = $columns['order-actions'];
    unset($columns['order-actions']);

    $columns['admin-notes'] = __('Admin Notes', 'woocommerce');
    $columns['order-actions'] = $column_actions;

    return $columns;
}

// CSS styles
add_action( 'wp_head', 'myaccount_admin_order_notes_inline_style', 100 );
function myaccount_admin_order_notes_inline_style() {
    if( is_account_page() && is_wc_endpoint_url('orders') ) :
    ?><style>ul.order-note-item {list-style:none; margin:0;} ul.order-note-item > li { margin:0 0 6px;}</style><?php
    endif;
}

// Admin orders list custom column displayed content
add_action( 'woocommerce_my_account_my_orders_column_admin-notes', 'add_myaccount_admin_order_notes_content' );
function add_myaccount_admin_order_notes_content( $order ) {
    $notes = wc_get_order_notes( array(
        'order_id' => $order->get_id(),
        'order_by' => 'date_created',
        'order' => 'ASC',
    ) );

    if( ! empty($notes) ) {
        $output = [];

        foreach( $notes as $note ) {
            if( $note->customer_note && 'system' !== $note->added_by ) {
                $output[] = sprintf( __('%s <br> %s'),
                    date_i18n( 'm/d/y H:i', strtotime( $note->date_created ) ),
                    $note->content
                );
            }
        }
        echo '<ul class="order-note-item"><li>'. implode('</li><li>', $output) .'</li></ul>';
    }
}

代码位于事件子主题(或事件主题)的functions.php 文件中。经过测试并有效。

enter image description here

关于php - 在 WooCommerce 我的帐户订单列表中添加发送给客户的管理订单备注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67269652/

相关文章:

php - Laravel 多重连接?

javascript - 如何在 Wordpress 中排队然后调用第二个 JQuery 版本

php - WooCommerce 中特定运输类别、最低金额和地理位置国家/地区的购物车消息

php - 将自定义列产品可见性添加到 Woocommerce 3 中的管理产品列表

wordpress - 结帐页面上的文件上传字段 woocommerce

php - $_GET 和 WordPress

php - 使用 Laravel 4 中的布局在不同 View 的标题中加载 CSS

php - Laravel - 选择要在表中显示的数据

css - 跳转到新行的导航下拉图标

php - WordPress数据库错误: You have an error in your SQL syntax