woocommerce_product_subcategories

Display product sub categories as thumbnails.

Description

(null|boolean) woocommerce_product_subcategories( (array) $args = array() ); 

Returns (null|boolean)

Parameters (1)

0. $args — Optional. (array) => array()
The args.

Usage

  1. if ( !function_exists( 'woocommerce_product_subcategories' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-template-functions.php'; 
  3.  
  4. // The args. 
  5. $args = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = woocommerce_product_subcategories($args); 
  9.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-template-functions.php  
  1. function woocommerce_product_subcategories( $args = array() ) { 
  2. global $wp_query; 
  3.  
  4. $defaults = array( 
  5. 'before' => '',  
  6. 'after' => '',  
  7. 'force_display' => false,  
  8. ); 
  9.  
  10. $args = wp_parse_args( $args, $defaults ); 
  11.  
  12. extract( $args ); 
  13.  
  14. // Main query only 
  15. if ( ! is_main_query() && ! $force_display ) { 
  16. return; 
  17.  
  18. // Don't show when filtering, searching or when on page > 1 and ensure we're on a product archive 
  19. if ( is_search() || is_filtered() || is_paged() || ( ! is_product_category() && ! is_shop() ) ) { 
  20. return; 
  21.  
  22. // Check categories are enabled 
  23. if ( is_shop() && '' === get_option( 'woocommerce_shop_page_display' ) ) { 
  24. return; 
  25.  
  26. // Find the category + category parent, if applicable 
  27. $term = get_queried_object(); 
  28. $parent_id = empty( $term->term_id ) ? 0 : $term->term_id; 
  29.  
  30. if ( is_product_category() ) { 
  31. $display_type = get_woocommerce_term_meta( $term->term_id, 'display_type', true ); 
  32.  
  33. switch ( $display_type ) { 
  34. case 'products' : 
  35. return; 
  36. break; 
  37. case '' : 
  38. if ( '' === get_option( 'woocommerce_category_archive_display' ) ) { 
  39. return; 
  40. break; 
  41.  
  42. // NOTE: using child_of instead of parent - this is not ideal but due to a WP bug ( https://core.trac.wordpress.org/ticket/15626 ) pad_counts won't work 
  43. $product_categories = get_categories( apply_filters( 'woocommerce_product_subcategories_args', array( 
  44. 'parent' => $parent_id,  
  45. 'menu_order' => 'ASC',  
  46. 'hide_empty' => 0,  
  47. 'hierarchical' => 1,  
  48. 'taxonomy' => 'product_cat',  
  49. 'pad_counts' => 1,  
  50. ) ) ); 
  51.  
  52. if ( apply_filters( 'woocommerce_product_subcategories_hide_empty', true ) ) { 
  53. $product_categories = wp_list_filter( $product_categories, array( 'count' => 0 ), 'NOT' ); 
  54.  
  55. if ( $product_categories ) { 
  56. echo $before; 
  57.  
  58. foreach ( $product_categories as $category ) { 
  59. wc_get_template( 'content-product_cat.php', array( 
  60. 'category' => $category,  
  61. ) ); 
  62.  
  63. // If we are hiding products disable the loop and pagination 
  64. if ( is_product_category() ) { 
  65. $display_type = get_woocommerce_term_meta( $term->term_id, 'display_type', true ); 
  66.  
  67. switch ( $display_type ) { 
  68. case 'subcategories' : 
  69. $wp_query->post_count = 0; 
  70. $wp_query->max_num_pages = 0; 
  71. break; 
  72. case '' : 
  73. if ( 'subcategories' === get_option( 'woocommerce_category_archive_display' ) ) { 
  74. $wp_query->post_count = 0; 
  75. $wp_query->max_num_pages = 0; 
  76. break; 
  77.  
  78. if ( is_shop() && 'subcategories' === get_option( 'woocommerce_shop_page_display' ) ) { 
  79. $wp_query->post_count = 0; 
  80. $wp_query->max_num_pages = 0; 
  81.  
  82. echo $after; 
  83.  
  84. return true;