get_payment_method() responded in fatal error

What is the issue?

While trying to query the orders from WooCommerce get_payment_method() method throws the fatal error below.

$_query_args = [
    'posts_per_page'    => -1,
    'orderby'           => 'date',
    'order'             => 'DESC',
];
    
$_orders_query = wc_get_orders( $_query_args );

foreach ( $_orders_query as $_order ) {
    print_r( $_order->get_payment_method() );
}

 

Fatal Error

Call to undefined method Automattic\WooCommerce\Admin\Overrides\OrderRefund::get_payment_method()

 

Solution

Instead of using the query above, adding `type` argument here fixes the issue. So, the desired query will be…

$_query_args = [
    'posts_per_page'    => -1,
    'orderby'           => 'date',
    'order'             => 'DESC',
    'type'              => 'shop_order'  // This explicitly queries only orders, not refunds or other post types
];

Complete Snippet

$_query_args = [
    'posts_per_page'    => -1,
    'orderby'           => 'date',
    'order'             => 'DESC',
    'type'              => 'shop_order'  // This explicitly queries only orders, not refunds or other post types
];

    
$_orders_query = wc_get_orders( $_query_args );


foreach ( $_orders_query as $_order ) {
    print_r( $_order->get_payment_method() );
}
Picture of devtarik

devtarik