【问题标题】:how to get customer public note only with order id如何仅使用订单 ID 获取客户公共注释
【发布时间】:2018-05-17 12:49:22
【问题描述】:

我正在编写一个插件,客户可以使用order id 跟踪订单状态/详细信息。

而且我只想向客户显示公共订单说明

但我找不到任何功能或方法来做到这一点。

这是我的代码,但此代码显示所有笔记包括私人和公共笔记:

*/
function woohez_get_all_order_notes( $order_id ){
    $order_notes    =   array();
    $args = array (
            'post_id'   => $order_id,
            'orderby'   => 'comment_ID',
            'order'     => 'DESC',
            'approve'   => 'approve',
            'type'      => 'order_note'
    );
    remove_filter ( 'comments_clauses', array (
            'WC_Comments',
            'exclude_order_comments'
    ), 10, 1 );

    $notes = get_comments ( $args );
    if ($notes) {
        foreach ( $notes as $note ) {
            $order_notes[]  = wpautop ( wptexturize ( wp_kses_post ( $note->comment_content ) ) );
        }
    } 

    return $order_notes;
}


$notes_array  =  woohez_get_all_order_notes( 209 );
if ( count( $notes_array ) != 0) {
    foreach ( $notes_array as $notes ){
        echo $notes; 
    }
} else {
    echo "No notes found!";
}

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    我解决了这个问题, 你应该添加meta keymeta value 就像这样:

    function woohez_get_all_order_notes( $orderNumber ){
                $order_notes    =   array();
                $args = array (
                        'post_id'   => $orderNumber,
                        'orderby'   => 'comment_ID',
                        'order'     => 'DESC',
                        'approve'   => 'approve',
                        'type'      => 'order_note',
                        'meta_query' => array(
                            array(
                              'key' => 'is_customer_note',
                              'value' => 1,
                              'compare' => 'EXISTS',
                            )
                          )
                );
                remove_filter ( 'comments_clauses', array (
                        'WC_Comments',
                        'exclude_order_comments'
                ), 10, 1 );
    
                $notes = get_comments ( $args );
                if ($notes) {
                    foreach ( $notes as $note ) {
                        $order_notes[]  = wpautop ( wptexturize ( wp_kses_post ( $note->comment_content ) ) );
                    }
                } 
    
                return $order_notes;
            }
    

    【讨论】:

      【解决方案2】:

      我认为下面的代码将获取私人笔记 ID 并将其从循环中排除,因此现在您将只获得非私人笔记

      */
      function woohez_get_all_order_notes( $order_id ){
          $private_order_notes = get_private_order_notes( $order_id );
          private_note_ids = array();
          foreach($private_order_notes as $private_note)
          {
                 $private_note_ids[] = $private_note['note_id'];
          }
          $order_notes    =   array();
          $args = array (
                  'post_id'   => $order_id,
                  'orderby'   => 'comment_ID',
                  'order'     => 'DESC',
                  'approve'   => 'approve',
                  'type'      => 'order_note',
                  'post__not_in' => $private_note_ids
          );
          remove_filter ( 'comments_clauses', array (
                  'WC_Comments',
                  'exclude_order_comments'
          ), 10, 1 );
      
          $notes = get_comments ( $args );
          if ($notes) {
              foreach ( $notes as $note ) {
                  $order_notes[]  = wpautop ( wptexturize ( wp_kses_post ( $note->comment_content ) ) );
              }
          } 
      
          return $order_notes;
      }
      
      
      $notes_array  =  woohez_get_all_order_notes( 209 );
      if ( count( $notes_array ) != 0) {
          foreach ( $notes_array as $notes ){
              echo $notes; 
          }
      } else {
          echo "No notes found!";
      }
      
      function get_private_order_notes( $order_id){
          global $wpdb;
      
          $table_perfixed = $wpdb->prefix . 'comments';
          $results = $wpdb->get_results("
              SELECT *
              FROM $table_perfixed
              WHERE  `comment_post_ID` = $order_id
              AND  `comment_type` LIKE  'order_note'
          ");
      
          foreach($results as $note){
              $order_note[]  = array(
                  'note_id'      => $note->comment_ID,
                  'note_date'    => $note->comment_date,
                  'note_author'  => $note->comment_author,
                  'note_content' => $note->comment_content,
              );
          }
          return $order_note;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-12
        • 2023-01-21
        • 2013-04-15
        • 2015-03-24
        • 2021-07-15
        • 2020-11-30
        • 2022-01-14
        相关资源
        最近更新 更多