在 WooCommerce 管理员订单列表上添加支付方式的过滤器下拉列表

Add a filter dropdown for payment method on WooCommerce admin orders list

阅读后 https://rudrastyh.com/woocommerce/columns.html 我能够在 WooCommerce 管理员订单列表中添加一个带有订单付款方式的列

add_filter('manage_edit-shop_order_columns', 'misha_order_items_column' );
function misha_order_items_column( $order_columns ) {
    $order_columns['order_payment_method'] = "Payment method";
    return $order_columns;
}

add_action( 'manage_shop_order_posts_custom_column' , 'misha_order_items_column_cnt' );
function misha_order_items_column_cnt( $colname ) {
    global $the_order; // the global order object

     if( $colname == 'order_payment_method' ) {

        // Get payment method
        $payment_method = $the_order->get_payment_method();

        echo $payment_method;
    }  
}

这非常有效,并在自定义列中添加了所需的状态


然后,根据我找到的代码,我进行了调整以添加过滤器

add_action('restrict_manage_posts', 'add_shop_order_filter_by_state');
function add_shop_order_filter_by_state(){
    global $pagenow, $typenow, $the_order;

    if( 'shop_order' === $typenow && 'edit.php' === $pagenow ) {
        // Get payment method
        $payment_method = $the_order->get_payment_method();

        // Initializing
        $filter_id   = 'payment_method';
        $current     = isset($_GET[$filter_id])? $_GET[$filter_id] : '';

        echo '<select name="'.$filter_id.'">
        <option value="">'.__( 'Filter by payment method', 'woocommerce' )."</option>";

        // Loop through shipping zones locations array
        foreach( $payment_method as $method ) {
            echo $method;
        }
        echo '</select>';
    }
}

add_filter( 'request', 'process_admin_shop_order_filtering_by_state', 99 );
function process_admin_shop_order_filtering_by_state( $vars ) {
    global $pagenow, $typenow;

    $filter_id = 'payment_method';

    if ( $pagenow == 'edit.php' && 'shop_order' === $typenow
    && isset( $_GET[$filter_id] ) && ! empty($_GET[$filter_id]) ) {
        $vars['meta_key']   = 'payment_method';
        $vars['meta_value'] = $_GET[$filter_id];
        $vars['orderby']    = 'meta_value';
    }
    return $vars;
}

但是由于这些更改,在我的代码的第二部分中,我的日志文件中出现错误:

"Uncaught Error: Call to a member function get_payment_method() on null".

好像是全局变量$the_order没有被识别?

虽然我没有立即想到如何以不同的方式应用它。任何建议将不胜感激

您已接近找到答案,但您的代码包含一些小错误:

所以你得到:

// Display new column on WooCommerce admin orders list (header)
function filter_manage_edit_shop_order_columns( $columns ) {    
    // Add new column after order status (4) column
    return array_slice( $columns, 0, 4, true )
    + array( 'order_payment_method' => __( 'Payment method', 'woocommerce' ) )
    + array_slice( $columns, 4, NULL, true );
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );

// Display details after order status column, on order admin list (populate the column)
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {    
    // Compare
    if ( $column == 'order_payment_method' ) {
        // Get order
        $order = wc_get_order( $post_id );
    
        // Get the payment method
        $payment_method = $order->get_payment_method();
        
        // NOT empty
        if ( ! empty ( $payment_method ) ) {
            echo ucfirst( $payment_method );
        } else {
            echo __( 'N/A', 'woocommerce' );
        }
    }
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );

// Add filter dropdown
function action_restrict_manage_posts( $post_type, $which ) {   
    global $pagenow;

    // Compare
    if ( $post_type === 'shop_order' && $pagenow === 'edit.php' ) {
        // Filter ID
        $filter_id  = 'filter-by-payment';
        
        $current    = isset( $_GET[$filter_id] ) ? $_GET[$filter_id] : '';
        
        // Get available gateways
        $available_gateways = WC()->payment_gateways->get_available_payment_gateways();

        // Create a drop-down list 
        echo '<select name="' . $filter_id . '">
        <option value="">' . __( 'Filter by payment method', 'woocommerce' ) . '</option>';

        foreach ( $available_gateways as $key => $available_gateway ) {
            printf( '<option %s value="%s">%s</option>', $key === $current ? 'selected="selected"' : '', $key, ucfirst( $key ) );
        }
        
        echo '</select>';
    }
}
add_action( 'restrict_manage_posts', 'action_restrict_manage_posts', 10, 2 );

// Filter request
function filter_request( $vars ) {
    global $pagenow, $typenow;

    // Filter ID
    $filter_id = 'filter-by-payment';

    // Only on WooCommerce admin orders list
    if ( $pagenow == 'edit.php' && 'shop_order' === $typenow && isset( $_GET[$filter_id] ) && ! empty( $_GET[$filter_id] ) ) {
        $vars['meta_key']   = '_payment_method';
        $vars['meta_value'] = $_GET[$filter_id];
        $vars['orderby']    = 'meta_value';
    }
    
    return $vars;
}
add_filter( 'request', 'filter_request', 10, 1 );

结果: