Before asking this question I noticed that there are other similar questions on Stack Overflow without an accepted answer. Although each issue has a different context, I believe there is something in common.
I found these questions but they didn’t help:
I need to make some changes to the WooCommerce core file, but I don’t want to touch the original files. So is there a way to override the functions of the files located in the includes folder?
Specifically, the file is this:
I need to modify this piece of code (lines 235 and 237), the match would be the last two printf
. My question is if this piece of code can be modified with some filter or functions in functions.php file, so if WooCommerce updates I don’t lose the changes.
/**
* Render column: order_status.
*/
protected function render_order_status_column() {
$tooltip = '';
$comment_count = get_comment_count( $this->object->get_id() );
$approved_comments_count = absint( $comment_count['approved'] );
if ( $approved_comments_count ) {
$latest_notes = wc_get_order_notes(
array(
'order_id' => $this->object->get_id(),
'limit' => 1,
'orderby' => 'date_created_gmt',
)
);
$latest_note = current( $latest_notes );
if ( isset( $latest_note->content ) && 1 === $approved_comments_count ) {
$tooltip = wc_sanitize_tooltip( $latest_note->content );
} elseif ( isset( $latest_note->content ) ) {
/* translators: %d: notes count */
$tooltip = wc_sanitize_tooltip( $latest_note->content . '<br/><small style="display:block">' . sprintf( _n( 'Plus %d other note', 'Plus %d other notes', ( $approved_comments_count - 1 ), 'woocommerce' ), $approved_comments_count - 1 ) . '</small>' );
} else {
/* translators: %d: notes count */
$tooltip = wc_sanitize_tooltip( sprintf( _n( '%d note', '%d notes', $approved_comments_count, 'woocommerce' ), $approved_comments_count ) );
}
}
if ( $tooltip ) {
printf( '<mark class="order-status %s tips" data-tip="%s"><span>%s</span></mark>', esc_attr( sanitize_html_class( 'status-' . $this->object->get_status() ) ), wp_kses_post( $tooltip ), esc_html( wc_get_order_status_name( $this->object->get_status() ) ) );
} else {
printf( '<mark class="order-status %s"><span>%s</span></mark>', esc_attr( sanitize_html_class( 'status-' . $this->object->get_status() ) ), esc_html( wc_get_order_status_name( $this->object->get_status() ) ) );
}
}
2
Answers
For your specific case you can override the whole class, if you look at the source code it starts with:
That means that when WC loads that .php file if the class already exists it exit and live with your implementation.
This is a good compromise between directly modifying a wc core file and having your chance to edit the code.
Be careful tho, wordpress doesn’t offer a real priority system when loading plugins, to have this override have effect you have to include the new file containing your implementation BEFORE wc loading time span.
The best dirty way is to create a plugin called something like "aa-my-wc-override" and make it an easy plugin that just includes a file containing the new WC_Admin_List_Table_Orders class implementation. I suggest this way because WordPress loads the plugin alphabetically.
Another possibile way, if you just need to edit some HTML, maybe to inject some .js file in the backend that takes care of the needed modification. Maybe using AJAX or pure JS, depending on your needs
What you can do is remove the original
order-status
column, and then add a new customorder-status
column in the same location:Then you take over the existing code, and adjust it where desired: