Question

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?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top