1

I'm looking for a way to highlight the admin order list line based on the order payment method. (specifically for COD - cash on delivery)

Based on Highlight Woocommerce admin orders list when order contains a regular product anwser code, I wrote the following code:

function add_custom_class( $classes, $class, $post_id ){
    // Check current screen and make sure you are only doing in order list page.
    $currentScreen = get_current_screen();
    if( $currentScreen->id === "edit-shop_order" ) {

        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );
        $has_cod = false;

        // Set Payment Gateway ID
        foreach ( $orders as $order){
            if ( $order->get_payment_method() === 'cod' ) {
                $has_cod = true;
                break;
            }
        }

        if( $has_cod ) {
            $classes[] = 'order-has-cod';
        }
    }

    return $classes;
}   
add_filter( 'post_class', 'add_custom_class', 10, 3 );

function add_custom_admin_css(){
    $currentScreen = get_current_screen();
    if( $currentScreen->id === "edit-shop_order" ) {
        ?>
        <style type="text/css">
            .order-has-cod{
                background-color: #a8fff6 !important; // here you have to your own color
            }
        </style>
        <?php
    }
}
add_action( 'admin_head', 'add_custom_admin_css', 10, 1 );

Unfortunately without the desired result. Any advice?

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
s011420
  • 33
  • 3

1 Answers1

0

Your code attempt contains some errors/mistakes:

  • $order_id is undefined when using $order = wc_get_order( $order_id ), $order_id should be replaced by $post_id
  • $orders is undefined
  • foreach() argument must be of type array|object, null given
  • You can use an if condition, specific for a certain payment method, but you can also just use the payment method itself as a class, and apply CSS on that basis for 1 or more payment methods if desired

So you get:

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 payment method
            $payment_method = $order->get_payment_method();

            // NOT empty
            if ( ! empty( $payment_method ) ) {
                $classes[] = $payment_method;
            }
        }
    }
 
    // 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.cod {
                background-color: #a8fff6 !important;
            }

            .type-shop_order.bacs {
                background-color: #e9b779 !important;
            } 

            .type-shop_order.cheque {
                background-color: #ccffc3 !important;
            } 
        </style>';
    }
}
add_action( 'admin_head', 'action_admin_head' );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50