-
-
Save ilyasfoo/2d614e01b2100f2b476e235bd4cdba06 to your computer and use it in GitHub Desktop.
Restore the old tooltip functionality in WooCommerce (show order comments).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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 ); |
Author
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
Example of the fix (obtained from this issue):
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)