'How to filter shop orders by the order price
my shop have 3 products with fixed prices ( 0$ , 18$ and 36$ ).
im trying to make a filter in the admin panel that will give me all the orders by a fixed price.
this is what i have tried so far :
/**
* Add price bulk filter for orders
*/
function add_filter_by_payment_price_orders() {
global $typenow;
if ( 'shop_order' === $typenow ) {
$amount_array = array(0,18,36);
?>
<select name="_shop_order_payment_price" id="dropdown_shop_order_payment_price">
<option value=""><?php esc_html_e( 'All Payment prices', 'text-domain' ); ?></option>
<?php foreach ( $amount_array as $key => $amount ) : ?>
<option value="<?php echo esc_attr( $key ); ?>" <?php echo esc_attr( isset( $_GET['_shop_order_payment_price'] ) ? selected( $key, $_GET['_shop_order_payment_price'], false ) : '' ); ?>>
<?php echo $amount ; ?>
</option>
<?php endforeach; ?>
</select>
<?php
}
}
add_action( 'restrict_manage_posts', 'add_filter_by_payment_price_orders', 99 );
/**
* Process bulk filter order for payment method
*
*/
function add_filter_by_payment_price_orders_query( $vars ) {
global $typenow;
if ( 'shop_order' === $typenow && isset( $_GET['_shop_order_payment_price'] ) ) {
$vars['meta_key'] = 'total';
$vars['meta_value'] = wc_clean( $_GET['_shop_order_payment_price'] );
}
return $vars;
}
add_filter( 'request', 'add_filter_by_payment_price_orders_query', 99 );
Solution 1:[1]
/**
* Add price bulk filter for orders
*/
function add_filter_by_payment_price_orders() {
global $typenow;
if ( 'shop_order' === $typenow ) {
$amount_array = array(0,18,36);
?>
<select name="_shop_order_payment_price" id="dropdown_shop_order_payment_price">
<option value=""><?php esc_html_e( 'All Payment prices', 'text-domain' ); ?></option>
<?php foreach ( $amount_array as $key => $amount ) : ?>
<option value="<?php echo esc_attr( $amount); ?>" <?php echo esc_attr( isset( $_GET['_shop_order_payment_price'] ) ? selected( $key, $_GET['_shop_order_payment_price'], false ) : '' ); ?>>
<?php echo $amount ; ?>
</option>
<?php endforeach; ?>
</select>
<?php
}
}
add_action( 'restrict_manage_posts', 'add_filter_by_payment_price_orders', 99 );
/**
* Process bulk filter order for payment method
*
*/
function add_filter_by_payment_price_orders_query( $vars ) {
global $typenow;
if ( 'shop_order' === $typenow && isset( $_GET['_shop_order_payment_price'] ) ) {
$vars['meta_key'] = '_order_total';
$vars['meta_value'] = wc_clean( $_GET['_shop_order_payment_price'] );
}
return $vars;
}
add_filter( 'request', 'add_filter_by_payment_price_orders_query', 99 );
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | davidasor |