Created
April 8, 2024 11:19
-
-
Save adczk/f0ac34eaac66e2d753133fdd765564be to your computer and use it in GitHub Desktop.
WooCommerce - filter orders by Payment Method if High Performance Order Storage option is active
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* add as MU plugin | |
Notes: | |
1. it ONLY works with Hight Performance Order Storage option enabled; | |
2. in line 37 there's plain $_GET used without any sanitization; that's insecure | |
so consider extending it to make sure no malicious data is inejected/use at your own risk | |
Tested with WooCommerce 8.7.0 (backwards/forward compatibility unknown | |
*/ | |
function add_filter_by_payment_method_orders() { | |
global $typenow; | |
// get all payment methods | |
$gateways = WC()->payment_gateways->payment_gateways(); | |
?> | |
<select name="_shop_order_payment_method" id="dropdown_shop_order_payment_method"> | |
<option value=""><?php esc_html_e( 'All Payment Methods', 'text-domain' ); ?></option> | |
<?php foreach ( $gateways as $id => $gateway ) : ?> | |
<option value="<?php echo esc_attr( $id ); ?>" <?php echo esc_attr( isset( $_GET['_shop_order_payment_method'] ) ? selected( $id, $_GET['_shop_order_payment_method'], false ) : '' ); ?>> | |
<?php echo esc_html( $gateway->get_method_title() ); ?> | |
</option> | |
<?php endforeach; ?> | |
</select> | |
<?php | |
} | |
add_action( 'woocommerce_order_list_table_restrict_manage_orders', 'add_filter_by_payment_method_orders', 99 ); | |
/** | |
* Process bulk filter order for payment method | |
* | |
*/ | |
function add_filter_by_payment_method_orders_query( $vars ) { | |
if ( isset( $_GET['_shop_order_payment_method'] ) ) { | |
$vars['payment_method'] = $_GET['_shop_order_payment_method']; | |
} | |
return $vars; | |
} | |
add_filter( 'woocommerce_order_query_args', 'add_filter_by_payment_method_orders_query', 99, 1 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment