Skip to content

Instantly share code, notes, and snippets.

@ChromeOrange
Created July 12, 2012 10:24
Show Gist options
  • Save ChromeOrange/3097233 to your computer and use it in GitHub Desktop.
Save ChromeOrange/3097233 to your computer and use it in GitHub Desktop.
custom_product_filter
<?php
add_filter( 'request', 'custom_product_filter' );
function custom_product_filter( $vars ) {
global $wpdb, $pagenow, $typenow, $wp_query, $woocommerce;
$product_check = '';
$product_check = $_GET['product_check'];
if ( $typenow == 'shop_order' && isset($product_check) && $product_check > 0 ) :
$order_id_list = array();
$order_id_list[] = 0;
$args = array(
'numberposts' => -1,
'orderby' => 'post_date',
'order' => 'desc',
'post_type' => 'shop_order',
'post_status' => 'publish'
);
$product_orders = get_posts( $args );
foreach ( $product_orders AS $product_order ) :
$order = &new woocommerce_order( $product_order->ID );
$items = (array) get_post_meta( $order->id, '_order_items', true );
foreach($items as $item) :
if ( $item[id] == $product_check ) :
$order_id_list[] = $order->id;
endif;
endforeach;
endforeach;
if ( $order_id_list ) :
$vars['post__in'] = $order_id_list;
endif;
endif;
return $vars;
}
add_action('restrict_manage_posts','woocommerce_orders_by_product');
function woocommerce_orders_by_product() {
global $typenow, $wp_query;
if ( $typenow == 'shop_order' ) :
$args = array(
'numberposts' => -1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'product',
'post_status' => 'publish'
);
$products = get_posts( $args );
$output = "<select name='product_check' id='dropdown_product_check'>";
$output .= '<option value="">Voucher Filter</option>';
foreach( $products as $product ) :
$output .= '<option value="' . $product->ID . '" ' . selected( $_GET['product_check'], $product->ID, false ) . '>' . $product->post_title . '</option>';
endforeach;
$output .= '</select>';
echo $output;
endif;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment