我正在寻找一种根据订单付款方式突出显示管理订单列表行的方法。 (专门针对 COD - 货到付款)
基于 当订单包含常规产品时突出显示 Woocommerce 管理订单列表 anwser 代码,我编写了以下代码:
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 );
不幸的是没有达到预期的结果。有什么建议吗?
您的代码尝试包含一些错误/错误:
$order_id
时,
$order = wc_get_order( $order_id )
未定义,$order_id
应替换为 $post_id
$orders
未定义foreach()
参数必须是 array|object 类型,给定 null 所以你得到:
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' );
感谢您的代码。它运行得很好,但在选择 hpos 后,它就停止了。我应该改变什么才能再次开始这项工作..