There have been many threads and topics over the past few years about how to create a custom comment type. For the most part it’s my understanding that it’s possible (because WooCommerce does it) but recommended to use comment_meta
instead.
My question is how does WooCommerce add the comment_type
of order_note
to the dropdown in Comment Administration?
A regrex of WooCommerce’s code turns up nothing helpful. Any direction is appreciated.
Example of adding a comment with the type of order_note
:
/**
* Adds a note (comment) to the order
*
* @access public
* @param string $note Note to add
* @param int $is_customer_note (default: 0) Is this a note for the customer?
* @return id Comment ID
*
* *file is class-wp-order.php*
*/
public function add_order_note( $note, $is_customer_note = 0 ) {
$is_customer_note = intval( $is_customer_note );
if ( is_user_logged_in() && current_user_can( 'manage_woocommerce' ) ) {
$user = get_user_by( 'id', get_current_user_id() );
$comment_author = $user->display_name;
$comment_author_email = $user->user_email;
} else {
$comment_author = __( 'WooCommerce', 'woocommerce' );
$comment_author_email = strtolower( __( 'WooCommerce', 'woocommerce' ) ) . '@';
$comment_author_email .= isset( $_SERVER['HTTP_HOST'] ) ? str_replace( 'www.', '', $_SERVER['HTTP_HOST'] ) : 'noreply.com';
$comment_author_email = sanitize_email( $comment_author_email );
}
$comment_post_ID = $this->id;
$comment_author_url = '';
$comment_content = $note;
$comment_agent = 'WooCommerce';
$comment_type = 'order_note';
$comment_parent = 0;
$comment_approved = 1;
$commentdata = apply_filters( 'woocommerce_new_order_note_data', compact( 'comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_agent', 'comment_type', 'comment_parent', 'comment_approved' ), array( 'order_id' => $this->id, 'is_customer_note' => $is_customer_note ) );
$comment_id = wp_insert_comment( $commentdata );
add_comment_meta( $comment_id, 'is_customer_note', $is_customer_note );
if ( $is_customer_note )
do_action( 'woocommerce_new_customer_note', array( 'order_id' => $this->id, 'customer_note' => $note ) );
return $comment_id;
}
Previous question: The project I’m working on right now would be much, much easier if I could just add a comment_type
in addition to comment_meta
where appropriate.
My question is where should I be looking for a guide/example on how to do this?
To answer your first question: »How does WooCommerce add the
comment_type
oforder_note
to the dropdown in Comment Administration?«. Fromwoocommerce-admin-init.php
:Of course you have to do your own, to avoid conflicts, like this:
Regarding your second, previous question: »Where should I be looking for a guide/example on how to do this?«. From the looks of it you can get a pretty good example out of the woocommerce files.
Besides that the functions you need are all documented, for example: wp_insert_comment, wp_update_comment and wp_delete_comment. It’s maybe not the most comprehensive part of the codex, but the important information is there, of course you can always look in the source: wp-includes/comment.php.
From what you wrote I’m assuming you know your way with
comment meta
, i.e.add_comment_meta()
,get_comment_meta()
,update_comment_meta()
anddelete_comment_meta()
– you can also find those in the codex, the source file is the same as linked above.Because you haven’t posted a real problem to solve, I’d say that’s just about it – of course that’s not it at all, but this should get you a good overview where to start.
You might want to take a look at @brasofilo’s answer to How to add filter in âCommentsâ at the admin panel?, for more information and some additional keywords to guide you.