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