WC_AJAX::add_order_note
Add order note via ajax.
Method of the class: WC_AJAX{}
Hooks from the method
Returns
null. Nothing (null).
Usage
$result = WC_AJAX::add_order_note();
WC_AJAX::add_order_note() WC AJAX::add order note code WC 10.9.1
<?php
public static function add_order_note() {
check_ajax_referer( 'add-order-note', 'security' );
if ( ! current_user_can( 'edit_shop_orders' ) || ! isset( $_POST['post_id'], $_POST['note'], $_POST['note_type'] ) ) {
wp_die( -1 );
}
$post_id = absint( $_POST['post_id'] );
$note = wp_kses_post( trim( wp_unslash( $_POST['note'] ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$note_type = wc_clean( wp_unslash( $_POST['note_type'] ) );
$is_customer_note = ( 'customer' === $note_type ) ? 1 : 0;
if ( $post_id > 0 ) {
$order = wc_get_order( $post_id );
$comment_id = $order->add_order_note( $note, $is_customer_note, true );
$note = wc_get_order_note( $comment_id );
if ( ! $note ) {
wp_die();
}
$note_classes = array( 'note' );
$note_classes[] = $is_customer_note ? 'customer-note' : '';
$note_classes = apply_filters( 'woocommerce_order_note_class', array_filter( $note_classes ), $note );
?>
<li rel="<?php echo absint( $note->id ); ?>" class="<?php echo esc_attr( implode( ' ', $note_classes ) ); ?>">
<div class="note_content">
<?php if ( $is_customer_note ) : ?>
<div class="note_header"><?php esc_html_e( 'Sent to customer', 'woocommerce' ); ?></div>
<?php endif; ?>
<div class="note_body">
<?php
$content = wc_wptexturize_order_note( $note->content );
echo wp_kses_post( wpautop( make_clickable( $content ) ) );
?>
</div>
</div>
<p class="meta">
<abbr class="exact-date" title="<?php echo esc_attr( $note->date_created->date( 'Y-m-d H:i:s' ) ); ?>">
<?php
/* translators: %1$s: order note date, %2$s: order note time */
printf( esc_html__( '%1$s at %2$s', 'woocommerce' ), esc_html( $note->date_created->date_i18n( wc_date_format() ) ), esc_html( $note->date_created->date_i18n( wc_time_format() ) ) );
?>
</abbr>
<?php
if ( 'system' !== $note->added_by ) :
/* translators: %s: order note author */
printf( ' ' . esc_html__( 'by %s', 'woocommerce' ), esc_html( $note->added_by ) );
endif;
?>
<?php
$note_date_label = $note->date_created->date_i18n( wc_date_format() );
$delete_aria_label = 'system' === $note->added_by
/* translators: %s: order note date */
? sprintf( __( 'Delete system note from %s', 'woocommerce' ), $note_date_label )
/* translators: %1$s: order note author, %2$s: order note date */
: sprintf( __( 'Delete note from %1$s on %2$s', 'woocommerce' ), $note->added_by, $note_date_label );
?>
<a href="#" class="delete_note" role="button" aria-label="<?php echo esc_attr( $delete_aria_label ); ?>"><?php esc_html_e( 'Delete', 'woocommerce' ); ?></a>
</p>
</li>
<?php
}
wp_die();
}