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