首页 > 解决方案 > 如何在 woocommerce 的查看订单页面中自定义订单备注

问题描述

请你能帮我找到如何编辑系统生成的自动订单注释(与通过订单更改的项目有关)我找不到它的文件

我想做的事

在此处输入图像描述

标签: phpwoocommercecode-snippets

解决方案


试试这个: 解决方案 1:
在结帐时添加订单备注

add_filter( 'woocommerce_checkout_fields', 'woo_custom_order_notes_checkout_fields' );
function woo_custom_order_notes_checkout_fields( $fields ) 
{
    $fields['order']['order_comments']['placeholder'] = 'Your Special notes';
    $fields['order']['order_comments']['label'] = 'Add your special note txt';

    return $fields;
}

当您使用自定义文本下订单并下订单后,您将能够在仪表板上的订单详细信息/编辑订单中查看自定义订单备注。

解决方案2:

单击更新按钮后,获取/打印订单查看/编辑页面上的所有注释。

// order comment/notes by id
function woo_get_comment_by_id( $comment_id ) {
    $comment = get_comment( intval( $comment_id ) );

    if ( ! empty( $comment ) ) {
        return $comment->comment_content;
    } else {
        return '';
    }
}

add_action( 'save_post_shop_order', 'wpo_wcol_order_notes', 10, 1 );

function wpo_wcol_order_notes ( $order_id ) {

    $args = array(
        'post_id' => $order_id,
        'orderby' => 'comment_ID',
        'order'   => 'DESC',
        'type'    => 'order_note',
    // 'number'  => 1
    );
    remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );

    $notes = get_comments( $args );

    if($notes){
        foreach($notes as $note){
            $comment_id = $note->comment_ID ?:0;
            if( $comment_id ){
                echo woo_get_comment_by_id( $comment_id ).'<br>';
            }
        }
    }
    exit; // after test you should removed
    add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
}

您还可以根据要求相应地更改操作“save_post_shop_order”。


推荐阅读