Category walker should be more efficient. closes #340.

This commit is contained in:
Mike Jolley 2011-12-08 00:22:38 +00:00
parent cf52e99bd5
commit c4b4086d8f
1 changed files with 47 additions and 69 deletions

View File

@ -494,88 +494,66 @@ function get_woocommerce_term_meta($term_id, $key, $single = true){
return get_metadata('woocommerce_term', $term_id, $key, $single);
}
/**
* WooCommerce Dropdown categories
*
* Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
* We use a custom walker, just like WordPress does it
*/
function woocommerce_product_dropdown_categories( $show_counts = 1, $hierarchal = 1 ) {
$terms = get_terms('product_cat', 'pad_counts=1&hierarchal='.$hierarchal.'&hide_empty=1&child_of=0');
if (!$terms) return;
$output = "<select name='product_cat' id='dropdown_product_cat'>";
$output .= '<option value="">'.__('Show all categories', 'woothemes').'</option>';
foreach($terms as $term) :
if ($hierarchal && $term->parent!=0) continue;
global $wp_query;
if ($hierarchal) $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";
if ($show_counts) $output .= " ($term->count)";
$output .= "</option>";
else :
$output .="<option value='$term->slug'>$depth$term->name";
if ($show_counts) $output .= " ($term->count)";
$output .= "</option>";
endif;
if ($hierarchal) $output .= woocommerce_get_product_category_children($term->term_id, $show_counts);
endforeach;
$r = array();
$r['pad_counts'] = 1;
$r['hierarchal'] = $hierarchal;
$r['hide_empty'] = 1;
$r['show_count'] = 1;
$r['selected'] = (isset($wp_query->query['product_cat'])) ? $wp_query->query['product_cat'] : '';
$terms = get_terms( 'product_cat', $r );
if (!$terms) return;
$output = "<select name='product_cat' id='dropdown_product_cat'>";
$output .= '<option value="">'.__('Show all categories', 'woothemes').'</option>';
$output .= woocommerce_walk_category_dropdown_tree( $terms, 0, $r );
$output .="</select>";
echo $output;
}
function woocommerce_get_product_category_depth( $id = '', $depth = '', $i = '', $show_counts = 1 ) {
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;
/**
* Walk the Product Categories.
*/
function woocommerce_walk_category_dropdown_tree() {
$args = func_get_args();
// the user's options are the third parameter
if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
$walker = new Woocommerce_Walker_CategoryDropdown;
else
$walker = $args[2]['walker'];
return call_user_func_array(array( &$walker, 'walk' ), $args );
}
function woocommerce_get_product_category_children( $id = '', $show_counts = 1 ) {
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));
/**
* Create HTML dropdown list of Product Categories.
*/
class Woocommerce_Walker_CategoryDropdown extends Walker {
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";
if ($show_counts) $output .= " ($term->count)";
$output .= "</option>";
else :
$output .="<option value='$term->slug'>$depth$term->name";
if ($show_counts) $output .= " ($term->count)";
$output .= "</option>";
endif;
$output .= woocommerce_get_product_category_children($term->term_id);
endforeach;
var $tree_type = 'category';
var $db_fields = array ('parent' => 'parent', 'id' => 'term_id', 'slug' => 'slug' );
return $output;
function start_el(&$output, $category, $depth, $args) {
$pad = str_repeat('&nbsp;', $depth * 3);
$cat_name = apply_filters('list_product_cats', $category->name, $category);
$output .= "\t<option class=\"level-$depth\" value=\"".$category->slug."\"";
if ( $category->slug == $args['selected'] )
$output .= ' selected="selected"';
$output .= '>';
$output .= $pad.$cat_name;
if ( $args['show_count'] )
$output .= '&nbsp;('. $category->count .')';
$output .= "</option>\n";
}
}