Agregue un menú desplegable de filtro para el método de pago en la lista de pedidos de administración de WooCommerce

4 minutos de lectura

Avatar de usuario de Jod
jod

Despues de leer https://rudrastyh.com/woocommerce/columns.html Pude agregar una columna con el método de pago del pedido en la lista de pedidos del administrador de 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;
    }  
}

Esto funciona perfectamente y agrega los estados deseados en la columna personalizada


Luego, basándome en un código que encontré, hice ajustes para agregar el filtro

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;
}

Pero debido a esos cambios, en la segunda parte de mi código tengo un error en mis archivos de registro:

“Error no detectado: llamada a una función miembro get_payment_method () en nulo”.

Parece que la variable global $the_order ¿no esta reconocido?

Aunque no tengo una idea inmediata de cómo puedo aplicar esto de manera diferente. Cualquier consejo sería apreciado

Avatar de usuario de 7uc1f3r
7uc1f3r

Está cerca en la búsqueda de su respuesta, sin embargo, su código contiene algunos pequeños errores:

Entonces obtienes:

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

Resultado:

resultado

¿Ha sido útil esta solución?