-1

I'm looking for a way to highlight the admin order list line based on the order shipping method. (specifically for local pickup)

Based on Highlight WooCommerce admin orders list based on order payment method anwser code, I have changed $payment_method for $shipping_method and .type-shop_order.cod for .type-shop_order.local_pickupcode for shipping methods but it did nothing in admin panel I have also tried using shipping method title instead of $shipping_method, but it did nothing.

i have also tried to get all classes with

public function get_shipping_classes() {
        if ( empty( $this->shipping_classes ) ) {
            $classes                = get_terms(
                'product_shipping_class',
                array(
                    'hide_empty' => '0',
                    'orderby'    => 'name',
                )
            );
            $this->shipping_classes = ! is_wp_error( $classes ) ? $classes : array();
        }
        return apply_filters( 'woocommerce_get_shipping_classes', $this->shipping_classes );
    }

but it hid all orders except local pickup.

Here is the code:

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
            $shipping_method = $order->get_shipping_method();
            
             //NOT empty
            if ( ! empty( $shipping_method ) ) {
                $classes[] = $shipping_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.local_pickup {
                background-color: #e9a5a5 !important;
            }
            
        </style>';
    }
}
add_action( 'admin_head', 'action_admin_head' );

Any advice how to change it please? i am not php coder so this is kinda google try thing for me :/

RaDium
  • 37
  • 5
  • "Doesn't work" as a problem description is not very useful. What have you done so far, to try and figure out where the problem lies? Have you checked what you got in `$shipping_method`? What does the rendered HTML look like? – CBroe Jan 25 '23 at 11:56
  • Not what I asked you. – CBroe Jan 25 '23 at 12:55
  • I meant, go check what the resulting HTML code looks like. "View source code" in your browser or something. _"i have updated my question"_ - and yet still not answered my question. Go and check what the variable `$shipping_method` actually contains, after `$shipping_method = $order->get_shipping_method();` – CBroe Jan 25 '23 at 13:02
  • i have used echo $shipping_method; but i can not see anything in source code. – RaDium Jan 25 '23 at 13:46
  • echo isn't a very good tool for debugging - if the variable contained an empty string or NULL, you'd get no visible output at all. `var_dump` also shows you type and length of a value. – CBroe Jan 25 '23 at 13:50
  • echo '
    ' , var_dump($shipping_method) , '
    '; returned every order on the screen with this formar: iedit author-self level-0 post-18165 type-shop_order status-wc-processing post-password-required hentry DPD kuriér na adresu">
    – RaDium Jan 25 '23 at 14:30
  • That makes little sense. Why should the _shipping method_ contain information such as `author-self level-0 post-18165`? That very much looks like the already set _classes_ that get passed into the filter to begin with. – CBroe Jan 25 '23 at 14:32
  • i have changed $shipping_method = $order->get_shipping_method(); to $shipping_method = $order->get_shipping_methods(); to be able to see all data, now i see ["method_id"]=> string(12) "local_pickup" but i have no idea what to do with that – RaDium Jan 25 '23 at 14:59
  • Use the array key `method_id` to access the value ...? – CBroe Jan 25 '23 at 15:09
  • thank you mate, i am not debugger and this is first time i did something like this :D i wanted to blame you for your answers, but u did get me there! – RaDium Jan 25 '23 at 15:18

1 Answers1

0

i have made it thanks to CBroe.

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 = @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: #e9a5a5 !important;
            }
            
        </style>';
    }
}
add_action( 'admin_head', 'action_admin_head' );
RaDium
  • 37
  • 5