/includes/widgets/class-wc-widget-product-categories.php

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