最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Highlight order in admin dashboard in woocommerce with specific shipping method - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Mar 31 at 17:18 LoicTheAztec 255k24 gold badges399 silver badges446 bronze badges asked Mar 31 at 16:25 RaDiumRaDium 596 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 2

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).

发布评论

评论列表(0)

  1. 暂无评论