'Highlight Woocommerce admin orders list when order contains a regular product

I am using Woocommerce and YITH Subscription plugin to sell a subscription product. But when people also order a regular product I often overlook that (~95% of the orders are subscriptions).

Is there a way I can highlight/style the respective line in the admin orders list when an order also contains a regular product so it doesn't get overlooked?



Solution 1:[1]

You can use post_class filter hooks to add class to a table row.

First, you have to check get_current_screen() to make sure that you are adding a class to the order list page.

Now you have to get order items and need to check the product is a subscription or not using product meta _ywsbs_subscription

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" ) {
        // Getting an instance of the WC_Order object from a defined ORDER ID
        $order = wc_get_order( $post_id ); 

        $has_regular = false;

        // Iterating through each "line" items in the order      
        foreach ($order->get_items() as $item_id => $item ) {
            $product = $item->get_product();
            if( $product && $product->is_type('simple') ){
                if( get_post_meta( $product->get_id(), '_ywsbs_subscription', true ) == 'no' || get_post_meta( $product->get_id(), '_ywsbs_subscription', true ) == '' ){
                    $has_regular = true;
                    break;
                }
            }
        }
        if( $has_regular ){
            $classes[] = 'order-has-simple';
        }

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

Now you have to use admin_head hooks that help you to add CSS on the admin side.

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

add_action( 'admin_head', 'add_custom_admin_css', 10, 1 );

Tested and works.

enter image description here

As per OP requested

enter image description here

Check products by category

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" ) {
        // Getting an instance of the WC_Order object from a defined ORDER ID
        $order = wc_get_order( $post_id ); 

        $has_regular = false;

        // Iterating through each "line" items in the order      
        foreach ($order->get_items() as $item_id => $item ) {
            $product = $item->get_product();
            if( $product && has_term( 'vergangene-ausgaben', 'product_cat', $product->get_id() ) ) {
                $has_regular = true;
                break;
            }               
        }
        if( $has_regular ){
            $classes[] = 'order-has-simple';
        }

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1