Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

470
Views
¿Obtener notas de pedido de un objeto Woocommerce Order / WC_Order?

Puedo agregar una nota de pedido (nota privada) con:

 $order->add_order_note($info_for_order);

Pero cuando traté de obtener los valores en alguna página con:

 get_comments(['post_id' => $order_id]) // or $order_object->get_customer_order_notes()

Simplemente devuelve una matriz vacía. Busqué en Google esto y no puedo encontrar el método para hacerlo.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Las notas de pedido (nota privada) solo están disponibles para el backend cuando se usa la función get_comments() .
Si observa el WC_Comments exclude_order_comments() , verá que las consultas de front-end se filtran con respecto a las notas de pedidos privados...

Entonces, el cambio es crear una función personalizada para obtener las notas de pedido privadas:

 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; }

El código va en el archivo function.php de su tema secundario activo (o tema) o también en cualquier archivo de complemento.

Este código está probado y funciona.


Uso (por ejemplo, $order_id = 6238 ) :

 $order_id = 6238; $order_notes = get_private_order_notes( $order_id ); foreach($order_notes as $note){ $note_id = $note['note_id']; $note_date = $note['note_date']; $note_author = $note['note_author']; $note_content = $note['note_content']; // Outputting each note content for the order echo '<p>'.$note_content.'</p>'; }
about 4 years ago · Santiago Trujillo Report

0

Hay documentación de Woohttps://docs.woocommerce.com/wc-apidocs/function-wc_get_order_notes.html

Ejemplo:

 wc_get_order_notes([ 'order_id' => $order->get_id(), 'type' => 'customer', ]);

Resultado

 Array ( [0] => stdClass Object ( [id] => 11 [date_created] => WC_DateTime Object ( [utc_offset:protected] => 3600 [date] => 2019-03-21 11:38:51.000000 [timezone_type] => 1 [timezone] => +00:00 ) [content] => Hi, blah blah blah [customer_note] => 1 [added_by] => admin ) )
about 4 years ago · Santiago Trujillo Report

0

Existe una mejor forma alternativa de obtener notas de pedido.

 /** * Get all approved WooCommerce order notes. * * @param int|string $order_id The order ID. * @return array $notes The order notes, or an empty array if none. */ function custom_get_order_notes( $order_id ) { remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) ); $comments = get_comments( array( 'post_id' => $order_id, 'orderby' => 'comment_ID', 'order' => 'DESC', 'approve' => 'approve', 'type' => 'order_note', ) ); $notes = wp_list_pluck( $comments, 'comment_content' ); add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) ); return $notes; }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!