Skip to content

Instantly share code, notes, and snippets.

@ilyasfoo
Forked from mino129/restore-old-tooltips.php
Last active August 26, 2024 12:47
Show Gist options
  • Select an option

  • Save ilyasfoo/2d614e01b2100f2b476e235bd4cdba06 to your computer and use it in GitHub Desktop.

Select an option

Save ilyasfoo/2d614e01b2100f2b476e235bd4cdba06 to your computer and use it in GitHub Desktop.
Restore the old tooltip functionality in WooCommerce (show order comments).
<?php
/**
* Plugin Name: Restore WooCommerce Order Status Tooltip
* Description: Restores the old tooltip functionality in WooCommerce to show order comments as it was before 9.1.0.
* Version: 1.0
* Author: mino129 & ilyasfoo
* Author URI: https://gist.github.com/ilyasfoo/2d614e01b2100f2b476e235bd4cdba06
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*
* @package WooCommerce_Order_Status_Tooltip
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Restores the old tooltip functionality for order status.
*
* @param array $status_names Array of order status names.
* @param WC_Order $order The order object.
* @return array Modified status names array.
*/
function wc_restore_order_status_tooltip( $status_names, $order ) {
$message = null;
$order_id = $order->get_id();
$order_status = $order->get_status();
$approved_comments_count = 0;
// Temporarily remove filter to get the actual comment count.
remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10 );
$comment_count = get_comment_count( $order_id );
add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10 );
$approved_comments_count = absint( $comment_count['approved'] );
if ( $approved_comments_count ) {
$latest_notes = wc_get_order_notes(
array(
'order_id' => $order_id,
'limit' => 1,
'orderby' => 'date_created_gmt',
)
);
$latest_note = current( $latest_notes );
if ( isset( $latest_note->content ) && 1 === $approved_comments_count ) {
$message = $latest_note->content;
} elseif ( isset( $latest_note->content ) ) {
$message = $latest_note->content . '<br/><small style="display:block">' . sprintf(
/* translators: %d: number of additional notes */
_n( 'Plus %d other note', 'Plus %d other notes', ( $approved_comments_count - 1 ), 'woocommerce' ),
$approved_comments_count - 1
) . '</small>';
} else {
$message = sprintf(
/* translators: %d: total number of notes */
_n( '%d note', '%d notes', $approved_comments_count, 'woocommerce' ),
$approved_comments_count
);
}
}
if ( $message ) {
$status_names[ $order_status ] = wc_sanitize_tooltip( $message );
}
return $status_names;
}
add_filter( 'woocommerce_get_order_status_labels', 'wc_restore_order_status_tooltip', 10, 2 );
@ilyasfoo
Copy link
Author

ilyasfoo commented Aug 26, 2024

Example of the fix (obtained from this issue):

image

Note: translations no longer work since the original strings are removed from WooCommerce plugin.

Co-authored by @mino129 and AI (Cursor & claude-3.5-sonnet)

@ilyasfoo
Copy link
Author

Note: Requires WooCommerce 9.2.0 or greater

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment