1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5
6 7 8 9 10 11 12 13 14
15 class WC_Admin_Taxonomies {
16
17 18 19
20 public function __construct() {
21
22
23 add_action( "create_term", array( $this, 'create_term' ), 5, 3 );
24 add_action( "delete_term", array( $this, 'delete_term' ), 5 );
25
26
27 add_action( 'product_cat_add_form_fields', array( $this, 'add_category_fields' ) );
28 add_action( 'product_cat_edit_form_fields', array( $this, 'edit_category_fields' ), 10, 2 );
29 add_action( 'created_term', array( $this, 'save_category_fields' ), 10, 3 );
30 add_action( 'edit_term', array( $this, 'save_category_fields' ), 10, 3 );
31
32
33 add_filter( 'manage_edit-product_cat_columns', array( $this, 'product_cat_columns' ) );
34 add_filter( 'manage_product_cat_custom_column', array( $this, 'product_cat_column' ), 10, 3 );
35
36
37 add_action( 'product_cat_pre_add_form', array( $this, 'product_cat_description' ) );
38 add_action( 'product_shipping_class_pre_add_form', array( $this, 'shipping_class_description' ) );
39 }
40
41 42 43 44 45 46 47 48 49
50 public function create_term( $term_id, $tt_id = '', $taxonomy = '' ) {
51 if ( $taxonomy != 'product_cat' && ! taxonomy_is_product_attribute( $taxonomy ) )
52 return;
53
54 $meta_name = taxonomy_is_product_attribute( $taxonomy ) ? 'order_' . esc_attr( $taxonomy ) : 'order';
55
56 update_woocommerce_term_meta( $term_id, $meta_name, 0 );
57 }
58
59 60 61 62 63 64 65
66 public function delete_term( $term_id ) {
67
68 $term_id = (int) $term_id;
69
70 if ( ! $term_id )
71 return;
72
73 global $wpdb;
74 $wpdb->query( "DELETE FROM {$wpdb->woocommerce_termmeta} WHERE `woocommerce_term_id` = " . $term_id );
75 }
76
77 78 79 80 81 82
83 public function add_category_fields() {
84 ?>
85 <div class="form-field">
86 <label for="display_type"><?php _e( 'Display type', 'woocommerce' ); ?></label>
87 <select id="display_type" name="display_type" class="postform">
88 <option value=""><?php _e( 'Default', 'woocommerce' ); ?></option>
89 <option value="products"><?php _e( 'Products', 'woocommerce' ); ?></option>
90 <option value="subcategories"><?php _e( 'Subcategories', 'woocommerce' ); ?></option>
91 <option value="both"><?php _e( 'Both', 'woocommerce' ); ?></option>
92 </select>
93 </div>
94 <div class="form-field">
95 <label><?php _e( 'Thumbnail', 'woocommerce' ); ?></label>
96 <div id="product_cat_thumbnail" style="float:left;margin-right:10px;"><img src="<?php echo wc_placeholder_img_src(); ?>" width="60px" height="60px" /></div>
97 <div style="line-height:60px;">
98 <input type="hidden" id="product_cat_thumbnail_id" name="product_cat_thumbnail_id" />
99 <button type="button" class="upload_image_button button"><?php _e( 'Upload/Add image', 'woocommerce' ); ?></button>
100 <button type="button" class="remove_image_button button"><?php _e( 'Remove image', 'woocommerce' ); ?></button>
101 </div>
102 <script type="text/javascript">
103
104
105 if ( ! jQuery('#product_cat_thumbnail_id').val() )
106 jQuery('.remove_image_button').hide();
107
108
109 var file_frame;
110
111 jQuery(document).on( 'click', '.upload_image_button', function( event ){
112
113 event.preventDefault();
114
115
116 if ( file_frame ) {
117 file_frame.open();
118 return;
119 }
120
121
122 file_frame = wp.media.frames.downloadable_file = wp.media({
123 title: '<?php _e( 'Choose an image', 'woocommerce' ); ?>',
124 button: {
125 text: '<?php _e( 'Use image', 'woocommerce' ); ?>',
126 },
127 multiple: false
128 });
129
130
131 file_frame.on( 'select', function() {
132 attachment = file_frame.state().get('selection').first().toJSON();
133
134 jQuery('#product_cat_thumbnail_id').val( attachment.id );
135 jQuery('#product_cat_thumbnail img').attr('src', attachment.url );
136 jQuery('.remove_image_button').show();
137 });
138
139
140 file_frame.open();
141 });
142
143 jQuery(document).on( 'click', '.remove_image_button', function( event ){
144 jQuery('#product_cat_thumbnail img').attr('src', '<?php echo wc_placeholder_img_src(); ?>');
145 jQuery('#product_cat_thumbnail_id').val('');
146 jQuery('.remove_image_button').hide();
147 return false;
148 });
149
150 </script>
151 <div class="clear"></div>
152 </div>
153 <?php
154 }
155
156 157 158 159 160 161 162
163 public function edit_category_fields( $term, $taxonomy ) {
164
165 $display_type = get_woocommerce_term_meta( $term->term_id, 'display_type', true );
166 $image = '';
167 $thumbnail_id = absint( get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true ) );
168 if ( $thumbnail_id )
169 $image = wp_get_attachment_thumb_url( $thumbnail_id );
170 else
171 $image = wc_placeholder_img_src();
172 ?>
173 <tr class="form-field">
174 <th scope="row" valign="top"><label><?php _e( 'Display type', 'woocommerce' ); ?></label></th>
175 <td>
176 <select id="display_type" name="display_type" class="postform">
177 <option value="" <?php selected( '', $display_type ); ?>><?php _e( 'Default', 'woocommerce' ); ?></option>
178 <option value="products" <?php selected( 'products', $display_type ); ?>><?php _e( 'Products', 'woocommerce' ); ?></option>
179 <option value="subcategories" <?php selected( 'subcategories', $display_type ); ?>><?php _e( 'Subcategories', 'woocommerce' ); ?></option>
180 <option value="both" <?php selected( 'both', $display_type ); ?>><?php _e( 'Both', 'woocommerce' ); ?></option>
181 </select>
182 </td>
183 </tr>
184 <tr class="form-field">
185 <th scope="row" valign="top"><label><?php _e( 'Thumbnail', 'woocommerce' ); ?></label></th>
186 <td>
187 <div id="product_cat_thumbnail" style="float:left;margin-right:10px;"><img src="<?php echo $image; ?>" width="60px" height="60px" /></div>
188 <div style="line-height:60px;">
189 <input type="hidden" id="product_cat_thumbnail_id" name="product_cat_thumbnail_id" value="<?php echo $thumbnail_id; ?>" />
190 <button type="submit" class="upload_image_button button"><?php _e( 'Upload/Add image', 'woocommerce' ); ?></button>
191 <button type="submit" class="remove_image_button button"><?php _e( 'Remove image', 'woocommerce' ); ?></button>
192 </div>
193 <script type="text/javascript">
194
195
196 var file_frame;
197
198 jQuery(document).on( 'click', '.upload_image_button', function( event ){
199
200 event.preventDefault();
201
202
203 if ( file_frame ) {
204 file_frame.open();
205 return;
206 }
207
208
209 file_frame = wp.media.frames.downloadable_file = wp.media({
210 title: '<?php _e( 'Choose an image', 'woocommerce' ); ?>',
211 button: {
212 text: '<?php _e( 'Use image', 'woocommerce' ); ?>',
213 },
214 multiple: false
215 });
216
217
218 file_frame.on( 'select', function() {
219 attachment = file_frame.state().get('selection').first().toJSON();
220
221 jQuery('#product_cat_thumbnail_id').val( attachment.id );
222 jQuery('#product_cat_thumbnail img').attr('src', attachment.url );
223 jQuery('.remove_image_button').show();
224 });
225
226
227 file_frame.open();
228 });
229
230 jQuery(document).on( 'click', '.remove_image_button', function( event ){
231 jQuery('#product_cat_thumbnail img').attr('src', '<?php echo wc_placeholder_img_src(); ?>');
232 jQuery('#product_cat_thumbnail_id').val('');
233 jQuery('.remove_image_button').hide();
234 return false;
235 });
236
237 </script>
238 <div class="clear"></div>
239 </td>
240 </tr>
241 <?php
242 }
243
244 245 246 247 248 249 250 251 252
253 public function save_category_fields( $term_id, $tt_id, $taxonomy ) {
254 if ( isset( $_POST['display_type'] ) )
255 update_woocommerce_term_meta( $term_id, 'display_type', esc_attr( $_POST['display_type'] ) );
256
257 if ( isset( $_POST['product_cat_thumbnail_id'] ) )
258 update_woocommerce_term_meta( $term_id, 'thumbnail_id', absint( $_POST['product_cat_thumbnail_id'] ) );
259
260 delete_transient( 'wc_term_counts' );
261 }
262
263 264 265 266 267 268
269 public function product_cat_description() {
270 echo wpautop( __( 'Product categories for your store can be managed here. To change the order of categories on the front-end you can drag and drop to sort them. To see more categories listed click the "screen options" link at the top of the page.', 'woocommerce' ) );
271 }
272
273 274 275 276 277 278
279 public function shipping_class_description() {
280 echo wpautop( __( 'Shipping classes can be used to group products of similar type. These groups can then be used by certain shipping methods to provide different rates to different products.', 'woocommerce' ) );
281 }
282
283 284 285 286 287 288 289
290 public function product_cat_columns( $columns ) {
291 $new_columns = array();
292 $new_columns['cb'] = $columns['cb'];
293 $new_columns['thumb'] = __( 'Image', 'woocommerce' );
294
295 unset( $columns['cb'] );
296
297 return array_merge( $new_columns, $columns );
298 }
299
300 301 302 303 304 305 306 307 308
309 public function product_cat_column( $columns, $column, $id ) {
310
311 if ( $column == 'thumb' ) {
312
313 $image = '';
314 $thumbnail_id = get_woocommerce_term_meta( $id, 'thumbnail_id', true );
315
316 if ($thumbnail_id)
317 $image = wp_get_attachment_thumb_url( $thumbnail_id );
318 else
319 $image = wc_placeholder_img_src();
320
321
322
323 $image = str_replace( ' ', '%20', $image );
324
325 $columns .= '<img src="' . esc_url( $image ) . '" alt="Thumbnail" class="wp-post-image" height="48" width="48" />';
326
327 }
328
329 return $columns;
330 }
331 }
332
333 new WC_Admin_Taxonomies();
334