Skip to content

Instantly share code, notes, and snippets.

@karpstrucking
Created January 6, 2016 20:27
Show Gist options
  • Save karpstrucking/aff3118f67344df8d0b0 to your computer and use it in GitHub Desktop.
Save karpstrucking/aff3118f67344df8d0b0 to your computer and use it in GitHub Desktop.
WooCommerce - Filter orders based on how the order was created (admin versus natural)
<?php
/*
Plugin Name: WooCommerce Filter by "Created Via"
Description: Filter orders based on how the order was created
Author: GoWP
Author URI: https://www.gowp.com
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
add_action( 'restrict_manage_posts', 'wcfcv_restrict_manage_posts' );
function wcfcv_restrict_manage_posts() {
if ( isset( $_REQUEST['post_type'] ) && ( 'shop_order' == $_REQUEST['post_type'] ) ) {
?>
<select name="created-via">
<option value="">Created via...</option>
<option value="checkout">Checkout</option>
<option value="other">Other</option>
</select>
<?php
}
}
add_filter( 'parse_query', 'wcfcv_parse_query' );
function wcfcv_parse_query( $query ) {
if (
is_admin()
&& isset( $_REQUEST['post_type'] ) && ( 'shop_order' == $_REQUEST['post_type'] )
&& isset( $_REQUEST['created-via'] ) && ! empty( $_REQUEST['created-via'] ) && ( $value = $_REQUEST['created-via'] )
) {
$compare = ( 'other' == $value ) ? "NOT EXISTS" : $value;
$meta_query = array(
array(
'key' => '_created_via',
'value' => $value,
'compare' => $compare
)
);
$query->set( 'meta_query', $meta_query );
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment