Overriding filters from classes in woocommerce

I am trying override a function from class WC_Admin_Post_Types and tried something like this:

plugins/customization/wc_admin_post_types_new.php

Read More
class WC_Admin_Post_Types_new {
    public function __construct() {
        add_filter( 'views_edit-product', array( $this, 'product_sorting_link_new' ) );
    }
    public function product_sorting_link_new( $views ) {
        global $post_type, $wp_query;

        if ( ! current_user_can('edit_others_pages') ) {
            return $views;
        }

        $class            = ( isset( $wp_query->query['orderby'] ) && $wp_query->query['orderby'] == 'menu_order title' ) ? 'current' : '';
        $query_string     = remove_query_arg(array( 'orderby', 'order' ));
        $query_string     = add_query_arg( 'orderby', urlencode('menu_order title'), $query_string );
        $query_string     = add_query_arg( 'order', urlencode('ASC'), $query_string );
        $views['byorder'] = '<a href="' . esc_url( $query_string ) . '" class="' . esc_attr( $class ) . '">' . __( 'Sort Courses', 'woocommerce' ) . '</a>';

        return $views;
    }
}
new WC_Admin_Post_Types_new();

Original class looks like this

plugins/woocommerce/includes/admin/class-wc-admin-post-types.php

class WC_Admin_Post_Types {
    public function __construct() {
        add_filter( 'views_edit-product', array( $this, 'product_sorting_link' ) );
    }
    public function product_sorting_link( $views ) {
        global $post_type, $wp_query;

        if ( ! current_user_can('edit_others_pages') ) {
            return $views;
        }

        $class            = ( isset( $wp_query->query['orderby'] ) && $wp_query->query['orderby'] == 'menu_order title' ) ? 'current' : '';
        $query_string     = remove_query_arg(array( 'orderby', 'order' ));
        $query_string     = add_query_arg( 'orderby', urlencode('menu_order title'), $query_string );
        $query_string     = add_query_arg( 'order', urlencode('ASC'), $query_string );
        $views['byorder'] = '<a href="' . esc_url( $query_string ) . '" class="' . esc_attr( $class ) . '">' . __( 'Sort Products', 'woocommerce' ) . '</a>';

        return $views;
    }
}
new WC_Admin_Post_Types();

I am trying to change

$views['byorder'] = '<a href="' . esc_url( $query_string ) . '" class="' . esc_attr( $class ) . '">' . __( 'Sort Products', 'woocommerce' ) . '</a>';

with

$views['byorder'] = '<a href="' . esc_url( $query_string ) . '" class="' . esc_attr( $class ) . '">' . __( 'Sort Courses', 'woocommerce' ) . '</a>';

Tried adding

remove_filter( 'views_edit-product', array( 'WC_Admin_Post_Types', 'product_sorting_link_new' ) );

in the __construct and outside the class, but always returned false.

So how can I remove this filter and replace Sort Products with Sort Courses without editing or without using .pot. Need to know because there will be more altering on different classes from woocommerce.

Related posts

Leave a Reply

2 comments

  1. For this I have written a little function:

    function my_text_strings( $translated_text, $text, $domain ) {
        switch ( $translated_text ) {
            case 'Sort Products' :
                $translated_text = __( 'Sort Courses', 'woocommerce' );
                break;
        }
        return $translated_text;
    }
    add_filter( 'gettext', 'my_text_strings', 20, 3 )
    

    However I find this method to be quite clean.

  2. Found a temporary fix, but still not able to do it by the book like remove the filter or action. I added this function outside the class, before the class and it worked overriding.

    function mfields_test_remove_actions() {
        var_dump(remove_filter( 'views_edit-product', array( 'WC_Admin_Post_Types', 'product_sorting_link_new' ) )); // still returnes false but add filter is overriding it.
        add_filter( 'views_edit-product', array( 'WC_Admin_Post_Types_new', 'product_sorting_link_new' ) );
    }
    add_action( 'admin_init', 'mfields_test_remove_actions' );