wordpress 基于订单支付方式高亮WooCommerce管理订单列表

bmp9r5qi  于 2023-01-25  发布在  WordPress
关注(0)|答案(1)|浏览(117)

我正在寻找一种方法,以突出显示的行政命令列表行的基础上,以支付方式。(特别是货到付款-现金交付)
基于Highlight Woocommerce admin orders list when order contains a regular product 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 );

很不幸没有达到预期的结果。有什么建议吗?

cyej8jka

cyej8jka1#

您的代码尝试包含一些错误:

  • 使用$order = wc_get_order( $order_id )$order_id未定义,应将$order_id替换为$post_id
  • $orders未定义
  • foreach()参数必须为数组类型|对象,给定空值
  • 您可以使用if条件,特定于某个支付方式,但是您也可以只将支付方式本身作为一个类使用,并在此基础上为一个或多个支付方式应用CSS(如果需要

因此,您将获得:

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' );

相关问题