WC_Widget_Product_Categories

Product Categories Widget.

Defined (1)

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

/includes/widgets/class-wc-widget-product-categories.php  
  1. class WC_Widget_Product_Categories extends WC_Widget { 
  2.  
  3. /** 
  4. * Category ancestors. 
  5. * @var array 
  6. */ 
  7. public $cat_ancestors; 
  8.  
  9. /** 
  10. * Current Category. 
  11. * @var bool 
  12. */ 
  13. public $current_cat; 
  14.  
  15. /** 
  16. * Constructor. 
  17. */ 
  18. public function __construct() { 
  19. $this->widget_cssclass = 'woocommerce widget_product_categories'; 
  20. $this->widget_description = __( 'A list or dropdown of product categories.', 'woocommerce' ); 
  21. $this->widget_id = 'woocommerce_product_categories'; 
  22. $this->widget_name = __( 'WooCommerce product categories', 'woocommerce' ); 
  23. $this->settings = array( 
  24. 'title' => array( 
  25. 'type' => 'text',  
  26. 'std' => __( 'Product categories', 'woocommerce' ),  
  27. 'label' => __( 'Title', 'woocommerce' ),  
  28. ),  
  29. 'orderby' => array( 
  30. 'type' => 'select',  
  31. 'std' => 'name',  
  32. 'label' => __( 'Order by', 'woocommerce' ),  
  33. 'options' => array( 
  34. 'order' => __( 'Category order', 'woocommerce' ),  
  35. 'name' => __( 'Name', 'woocommerce' ),  
  36. ),  
  37. ),  
  38. 'dropdown' => array( 
  39. 'type' => 'checkbox',  
  40. 'std' => 0,  
  41. 'label' => __( 'Show as dropdown', 'woocommerce' ),  
  42. ),  
  43. 'count' => array( 
  44. 'type' => 'checkbox',  
  45. 'std' => 0,  
  46. 'label' => __( 'Show product counts', 'woocommerce' ),  
  47. ),  
  48. 'hierarchical' => array( 
  49. 'type' => 'checkbox',  
  50. 'std' => 1,  
  51. 'label' => __( 'Show hierarchy', 'woocommerce' ),  
  52. ),  
  53. 'show_children_only' => array( 
  54. 'type' => 'checkbox',  
  55. 'std' => 0,  
  56. 'label' => __( 'Only show children of the current category', 'woocommerce' ),  
  57. ),  
  58. 'hide_empty' => array( 
  59. 'type' => 'checkbox',  
  60. 'std' => 0,  
  61. 'label' => __( 'Hide empty categories', 'woocommerce' ),  
  62. ),  
  63. ); 
  64.  
  65. parent::__construct(); 
  66.  
  67. /** 
  68. * Output widget. 
  69. * @see WP_Widget 
  70. * @param array $args 
  71. * @param array $instance 
  72. */ 
  73. public function widget( $args, $instance ) { 
  74. global $wp_query, $post; 
  75.  
  76. $count = isset( $instance['count'] ) ? $instance['count'] : $this->settings['count']['std']; 
  77. $hierarchical = isset( $instance['hierarchical'] ) ? $instance['hierarchical'] : $this->settings['hierarchical']['std']; 
  78. $show_children_only = isset( $instance['show_children_only'] ) ? $instance['show_children_only'] : $this->settings['show_children_only']['std']; 
  79. $dropdown = isset( $instance['dropdown'] ) ? $instance['dropdown'] : $this->settings['dropdown']['std']; 
  80. $orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : $this->settings['orderby']['std']; 
  81. $hide_empty = isset( $instance['hide_empty'] ) ? $instance['hide_empty'] : $this->settings['hide_empty']['std']; 
  82. $dropdown_args = array( 'hide_empty' => $hide_empty ); 
  83. $list_args = array( 'show_count' => $count, 'hierarchical' => $hierarchical, 'taxonomy' => 'product_cat', 'hide_empty' => $hide_empty ); 
  84.  
  85. // Menu Order 
  86. $list_args['menu_order'] = false; 
  87. if ( 'order' === $orderby ) { 
  88. $list_args['menu_order'] = 'asc'; 
  89. } else { 
  90. $list_args['orderby'] = 'title'; 
  91.  
  92. // Setup Current Category 
  93. $this->current_cat = false; 
  94. $this->cat_ancestors = array(); 
  95.  
  96. if ( is_tax( 'product_cat' ) ) { 
  97.  
  98. $this->current_cat = $wp_query->queried_object; 
  99. $this->cat_ancestors = get_ancestors( $this->current_cat->term_id, 'product_cat' ); 
  100.  
  101. } elseif ( is_singular( 'product' ) ) { 
  102.  
  103. $product_category = wc_get_product_terms( $post->ID, 'product_cat', apply_filters( 'woocommerce_product_categories_widget_product_terms_args', array( 'orderby' => 'parent' ) ) ); 
  104.  
  105. if ( ! empty( $product_category ) ) { 
  106. $this->current_cat = end( $product_category ); 
  107. $this->cat_ancestors = get_ancestors( $this->current_cat->term_id, 'product_cat' ); 
  108.  
  109. // Show Siblings and Children Only 
  110. if ( $show_children_only && $this->current_cat ) { 
  111.  
  112. // Top level is needed 
  113. $top_level = get_terms( 
  114. 'product_cat',  
  115. array( 
  116. 'fields' => 'ids',  
  117. 'parent' => 0,  
  118. 'hierarchical' => true,  
  119. 'hide_empty' => false,  
  120. ); 
  121.  
  122. // Direct children are wanted 
  123. $direct_children = get_terms( 
  124. 'product_cat',  
  125. array( 
  126. 'fields' => 'ids',  
  127. 'parent' => $this->current_cat->term_id,  
  128. 'hierarchical' => true,  
  129. 'hide_empty' => false,  
  130. ); 
  131.  
  132. // Gather siblings of ancestors 
  133. $siblings = array(); 
  134. if ( $this->cat_ancestors ) { 
  135. foreach ( $this->cat_ancestors as $ancestor ) { 
  136. $ancestor_siblings = get_terms( 
  137. 'product_cat',  
  138. array( 
  139. 'fields' => 'ids',  
  140. 'parent' => $ancestor,  
  141. 'hierarchical' => false,  
  142. 'hide_empty' => false,  
  143. ); 
  144. $siblings = array_merge( $siblings, $ancestor_siblings ); 
  145.  
  146. if ( $hierarchical ) { 
  147. $include = array_merge( $top_level, $this->cat_ancestors, $siblings, $direct_children, array( $this->current_cat->term_id ) ); 
  148. } else { 
  149. $include = array_merge( $direct_children ); 
  150.  
  151. $dropdown_args['include'] = implode( ', ', $include ); 
  152. $list_args['include'] = implode( ', ', $include ); 
  153.  
  154. if ( empty( $include ) ) { 
  155. return; 
  156. } elseif ( $show_children_only ) { 
  157. $dropdown_args['depth'] = 1; 
  158. $dropdown_args['child_of'] = 0; 
  159. $dropdown_args['hierarchical'] = 1; 
  160. $list_args['depth'] = 1; 
  161. $list_args['child_of'] = 0; 
  162. $list_args['hierarchical'] = 1; 
  163.  
  164. $this->widget_start( $args, $instance ); 
  165.  
  166. // Dropdown 
  167. if ( $dropdown ) { 
  168. $dropdown_defaults = array( 
  169. 'show_count' => $count,  
  170. 'hierarchical' => $hierarchical,  
  171. 'show_uncategorized' => 0,  
  172. 'orderby' => $orderby,  
  173. 'selected' => $this->current_cat ? $this->current_cat->slug : '',  
  174. ); 
  175. $dropdown_args = wp_parse_args( $dropdown_args, $dropdown_defaults ); 
  176.  
  177. // Stuck with this until a fix for https://core.trac.wordpress.org/ticket/13258 
  178. wc_product_dropdown_categories( apply_filters( 'woocommerce_product_categories_widget_dropdown_args', $dropdown_args ) ); 
  179.  
  180. wc_enqueue_js( " 
  181. jQuery( '.dropdown_product_cat' ).change( function() { 
  182. if ( jQuery(this).val() != '' ) { 
  183. var this_page = ''; 
  184. var home_url = '" . esc_js( home_url( '/' ) ) . "'; 
  185. if ( home_url.indexOf( '?' ) > 0 ) { 
  186. this_page = home_url + '&product_cat=' + jQuery(this).val(); 
  187. } else { 
  188. this_page = home_url + '?product_cat=' + jQuery(this).val(); 
  189. location.href = this_page; 
  190. }); 
  191. " ); 
  192.  
  193. // List 
  194. } else { 
  195.  
  196. include_once( WC()->plugin_path() . '/includes/walkers/class-product-cat-list-walker.php' ); 
  197.  
  198. $list_args['walker'] = new WC_Product_Cat_List_Walker; 
  199. $list_args['title_li'] = ''; 
  200. $list_args['pad_counts'] = 1; 
  201. $list_args['show_option_none'] = __( 'No product categories exist.', 'woocommerce' ); 
  202. $list_args['current_category'] = ( $this->current_cat ) ? $this->current_cat->term_id : ''; 
  203. $list_args['current_category_ancestors'] = $this->cat_ancestors; 
  204.  
  205. echo '<ul class="product-categories">'; 
  206.  
  207. wp_list_categories( apply_filters( 'woocommerce_product_categories_widget_args', $list_args ) ); 
  208.  
  209. echo '</ul>'; 
  210.  
  211. $this->widget_end( $args );