Not the nicest way to do nested categories, but better than rewriting the walkers

This commit is contained in:
Mike Jolley 2011-11-03 11:48:59 +00:00
parent 66f5a6768c
commit 3f247b2c18
1 changed files with 58 additions and 4 deletions

View File

@ -294,28 +294,82 @@ function woocommerce_custom_product_orderby( $vars ) {
}
/**
* Filter products by category
* Filter products by category, uses slugs for option values. Code adapted by Andrew Benbow - chromeorange.co.uk
**/
add_action('restrict_manage_posts','woocommerce_products_by_category');
function woocommerce_products_by_category() {
global $typenow, $wp_query;
if ($typenow=='product') :
$terms = get_terms('product_cat', 'pad_counts=1&hierarchal=1');
$terms = get_terms('product_cat', 'pad_counts=1&hierarchal=1&hide_empty=1&child_of=0');
$output = "<select name='product_cat' id='dropdown_product_cat'>";
$output .= '<option value="">'.__('Show all categories', 'woothemes').'</option>';
foreach($terms as $term) :
if ($term->parent!=0) continue;
$depth = woocommerce_get_product_category_depth($term->term_id);
if ( isset( $wp_query->query['product_cat'] ) ) :
$output .="<option value='$term->slug' ".selected($term->slug, $wp_query->query['product_cat'], false).">$term->name ($term->count)</option>";
$output .="<option value='$term->slug' ".selected($term->slug, $wp_query->query['product_cat'], false).">$depth$term->name ($term->count)</option>";
else :
$output .="<option value='$term->slug'>$term->name ($term->count)</option>";
$output .="<option value='$term->slug'>$depth$term->name ($term->count)</option>";
endif;
$output .= woocommerce_get_product_category_children($term->term_id);
endforeach;
$output .="</select>";
echo $output;
endif;
}
function woocommerce_get_product_category_depth( $id = '', $depth = '', $i = '' ) {
global $wpdb, $term_taxonomy;
if( $depth == '' ) :
if( $id == '' ) $id = $term_taxonomy->term_id;
$depth = $wpdb->get_var($wpdb->prepare("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '%s'", $id));
return woocommerce_get_product_category_depth($id, $depth, $i);
elseif( $depth == "0" ) :
return $i;
else :
$depth = $wpdb->get_var($wpdb->prepare("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '%s'", $depth));
$i .='&nbsp;&nbsp;&nbsp;';
return woocommerce_get_product_category_depth($id, $depth, $i);
endif;
}
function woocommerce_get_product_category_children( $id = '' ) {
global $wp_query;
if (!$id) return;
$output = '';
$terms = get_terms('product_cat', 'pad_counts=1&hierarchal=1&hide_empty=1&child_of='.esc_attr($id));
foreach( $terms as $term ) :
if ($term->parent!=$id) continue;
$depth = woocommerce_get_product_category_depth($term->term_id);
if ( isset( $wp_query->query['product_cat'] ) ) :
$output .="<option value='$term->slug' ".selected($term->slug, $wp_query->query['product_cat'], false).">$depth$term->name ($term->count)</option>";
else :
$output .="<option value='$term->slug'>$depth$term->name ($term->count)</option>";
endif;
$output .= woocommerce_get_product_category_children($term->term_id);
endforeach;
return $output;
}
/**
* Filter products by type