Skip to content

Instantly share code, notes, and snippets.

@simonlk
Created October 21, 2012 06:33
Show Gist options
  • Select an option

  • Save simonlk/3926139 to your computer and use it in GitHub Desktop.

Select an option

Save simonlk/3926139 to your computer and use it in GitHub Desktop.

Revisions

  1. simonlk revised this gist Oct 21, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Woocommerce - show empty categories
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // Paste this in to your theme's functions file
    // Redefine sub category display to output empty categories
    function woocommerce_product_subcategories( $args = array() ) {
    global $woocommerce, $wp_query, $_chosen_attributes;
  2. @invalid-email-address Anonymous created this gist Oct 21, 2012.
    81 changes: 81 additions & 0 deletions Woocommerce - show empty categories
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    // Redefine sub category display to output empty categories
    function woocommerce_product_subcategories( $args = array() ) {
    global $woocommerce, $wp_query, $_chosen_attributes;

    $defaults = array(
    'before' => '',
    'after' => '',
    'force_display' => false
    );

    $args = wp_parse_args( $args, $defaults );

    extract( $args );

    // Main query only
    if ( ! is_main_query() && ! $force_display ) return;

    // Don't show when filtering
    if ( sizeof( $_chosen_attributes ) > 0 || ( isset( $_GET['max_price'] ) && isset( $_GET['min_price'] ) ) ) return;

    // Don't show when searching or when on page > 1 and ensure we're on a product archive
    if ( is_search() || is_paged() || ( ! is_product_category() && ! is_shop() ) ) return;

    // Check cateogries are enabled
    if ( is_product_category() && get_option( 'woocommerce_show_subcategories' ) == 'no' ) return;
    if ( is_shop() && get_option( 'woocommerce_shop_show_subcategories' ) == 'no' ) return;

    // Find the category + category parent, if applicable
    if ( $product_cat_slug = get_query_var( 'product_cat' ) ) {
    $product_cat = get_term_by( 'slug', $product_cat_slug, 'product_cat' );
    $product_category_parent = $product_cat->term_id;
    } else {
    $product_category_parent = 0;
    }

    // NOTE: using child_of instead of parent - this is not ideal but due to a WP bug ( http://core.trac.wordpress.org/ticket/15626 ) pad_counts won't work
    $args = array(
    'child_of' => $product_category_parent,
    'menu_order' => 'ASC',
    'hide_empty' => 0,
    'hierarchical' => 1,
    'taxonomy' => 'product_cat',
    'pad_counts' => 1
    );
    $product_categories = get_categories( $args );

    $product_category_found = false;

    if ( $product_categories ) {

    foreach ( $product_categories as $category ) {

    if ( $category->parent != $product_category_parent )
    continue;

    if ( ! $product_category_found ) {
    // We found a category
    $product_category_found = true;
    echo $before;
    }

    woocommerce_get_template( 'content-product_cat.php', array(
    'category' => $category
    ) );

    }

    }

    // If we are hiding products disable the loop and pagination
    if ( $product_category_found == true && get_option( 'woocommerce_hide_products_when_showing_subcategories' ) == 'yes' ) {
    $wp_query->post_count = 0;
    $wp_query->max_num_pages = 0;
    }

    if ( $product_category_found ) {
    echo $after;
    return true;
    }

    }