phpcsswoocommerceadminorders

Highlight order in admin dashboard in woocommerce with specific shipping method


i have more web sites, i have tried to copy a code which is working on one ( same woo version, same php version, same theme, same hosting) to another one, but it is not working.

// **********local pickup orders list color**********

function filter_post_class( $classes, $class, $post_id ) {
    // Determines whether the current request is for an administrative interface page
    if ( ! is_admin() ) return $classes;

    // Get the current screen object
    $current_screen = get_current_screen();

    // Only when
    if ( $current_screen->id === 'edit-shop_order' ) {
        // Get an instance of the WC_Order object
        $order = wc_get_order( $post_id );

        // Is a WC_Order
        if ( is_a( $order, 'WC_Order' ) ) {
            // Get the shipping method
            // $shipping_method = $order->get_shipping_methods();
            $shipping_method = @array_shift($order->get_shipping_methods());
            $shipping_method_id = $shipping_method['method_id'];

             //NOT empty
            if ( ! empty( $shipping_method ) ) {
                $classes[] = $shipping_method_id;
            }
        }
    }

    // Return the array
    return $classes;
}
add_filter( 'post_class', 'filter_post_class', 10, 3 );

// Add CSS
function action_admin_head() {
    // Get the current screen object
    $current_screen = get_current_screen();

    // Only when
    if ( $current_screen->id === 'edit-shop_order' ) {
        echo '<style>
            .type-shop_order.local_pickup {
                background-color: #d5d6ad !important;
            }

        </style>';
    }
}
add_action( 'admin_head', 'action_admin_head' );

i have tried also this one :

WooCommerce dashboard - highlight orders marked 'Local pickup'

but none are working.

any idea why is this happening and how to fix it?


Solution

  • Note that WordPress post_class filter hook doesn't work when WooCommerce High Performance Order Storage (HPOS) is enabled, which seems to be the case on one of your websites, as HPOS is now enabled by default in WooCommerce.

    For Admin Orders hooks compatibility, see: Filtering orders list in WooCommerce with HPOS

    Here is the list of threads related to WooCommerce High Performance Order Storage on stack overflow.

    Alternative for HPOS:

    You could add a column to admin orders (HPOS enabled) with the shipping method used and highlight Local pickup directly in the code, adding some html tag with a style attribute with the desired styles to be applied (depending on the shipping method).