class-wc-widget-brand-description.php
1 year ago
class-wc-widget-brand-nav.php
7 months ago
class-wc-widget-brand-thumbnails.php
1 year ago
class-wc-widget-cart.php
3 years ago
class-wc-widget-layered-nav-filters.php
2 years ago
class-wc-widget-layered-nav.php
3 months ago
class-wc-widget-price-filter.php
8 months ago
class-wc-widget-product-categories.php
7 months ago
class-wc-widget-product-search.php
5 years ago
class-wc-widget-product-tag-cloud.php
5 years ago
class-wc-widget-products.php
1 year ago
class-wc-widget-rating-filter.php
5 years ago
class-wc-widget-recent-reviews.php
2 months ago
class-wc-widget-recently-viewed.php
1 year ago
class-wc-widget-top-rated-products.php
5 years ago
class-wc-widget-product-categories.php
302 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Product Categories Widget |
| 4 | * |
| 5 | * @package WooCommerce\Widgets |
| 6 | * @version 2.3.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * Product categories widget class. |
| 13 | * |
| 14 | * @extends WC_Widget |
| 15 | */ |
| 16 | class WC_Widget_Product_Categories extends WC_Widget { |
| 17 | |
| 18 | /** |
| 19 | * Category ancestors. |
| 20 | * |
| 21 | * @var array |
| 22 | */ |
| 23 | public $cat_ancestors; |
| 24 | |
| 25 | /** |
| 26 | * Current Category. |
| 27 | * |
| 28 | * @var bool |
| 29 | */ |
| 30 | public $current_cat; |
| 31 | |
| 32 | /** |
| 33 | * Constructor. |
| 34 | */ |
| 35 | public function __construct() { |
| 36 | $this->widget_cssclass = 'woocommerce widget_product_categories'; |
| 37 | $this->widget_description = __( 'A list or dropdown of product categories.', 'woocommerce' ); |
| 38 | $this->widget_id = 'woocommerce_product_categories'; |
| 39 | $this->widget_name = __( 'Product Categories', 'woocommerce' ); |
| 40 | $this->settings = array( |
| 41 | 'title' => array( |
| 42 | 'type' => 'text', |
| 43 | 'std' => __( 'Product categories', 'woocommerce' ), |
| 44 | 'label' => __( 'Title', 'woocommerce' ), |
| 45 | ), |
| 46 | 'orderby' => array( |
| 47 | 'type' => 'select', |
| 48 | 'std' => 'name', |
| 49 | 'label' => __( 'Order by', 'woocommerce' ), |
| 50 | 'options' => array( |
| 51 | 'order' => __( 'Category order', 'woocommerce' ), |
| 52 | 'name' => __( 'Name', 'woocommerce' ), |
| 53 | ), |
| 54 | ), |
| 55 | 'dropdown' => array( |
| 56 | 'type' => 'checkbox', |
| 57 | 'std' => 0, |
| 58 | 'label' => __( 'Show as dropdown', 'woocommerce' ), |
| 59 | ), |
| 60 | 'count' => array( |
| 61 | 'type' => 'checkbox', |
| 62 | 'std' => 0, |
| 63 | 'label' => __( 'Show product counts', 'woocommerce' ), |
| 64 | ), |
| 65 | 'hierarchical' => array( |
| 66 | 'type' => 'checkbox', |
| 67 | 'std' => 1, |
| 68 | 'label' => __( 'Show hierarchy', 'woocommerce' ), |
| 69 | ), |
| 70 | 'show_children_only' => array( |
| 71 | 'type' => 'checkbox', |
| 72 | 'std' => 0, |
| 73 | 'label' => __( 'Only show children of the current category', 'woocommerce' ), |
| 74 | ), |
| 75 | 'hide_empty' => array( |
| 76 | 'type' => 'checkbox', |
| 77 | 'std' => 0, |
| 78 | 'label' => __( 'Hide empty categories', 'woocommerce' ), |
| 79 | ), |
| 80 | 'max_depth' => array( |
| 81 | 'type' => 'text', |
| 82 | 'std' => '', |
| 83 | 'label' => __( 'Maximum depth', 'woocommerce' ), |
| 84 | ), |
| 85 | ); |
| 86 | |
| 87 | parent::__construct(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Output widget. |
| 92 | * |
| 93 | * @see WP_Widget |
| 94 | * @param array $args Widget arguments. |
| 95 | * @param array $instance Widget instance. |
| 96 | */ |
| 97 | public function widget( $args, $instance ) { |
| 98 | global $wp_query, $post; |
| 99 | |
| 100 | $count = isset( $instance['count'] ) ? $instance['count'] : $this->settings['count']['std']; |
| 101 | $hierarchical = isset( $instance['hierarchical'] ) ? $instance['hierarchical'] : $this->settings['hierarchical']['std']; |
| 102 | $show_children_only = isset( $instance['show_children_only'] ) ? $instance['show_children_only'] : $this->settings['show_children_only']['std']; |
| 103 | $dropdown = isset( $instance['dropdown'] ) ? $instance['dropdown'] : $this->settings['dropdown']['std']; |
| 104 | $orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : $this->settings['orderby']['std']; |
| 105 | $hide_empty = isset( $instance['hide_empty'] ) ? $instance['hide_empty'] : $this->settings['hide_empty']['std']; |
| 106 | $dropdown_args = array( |
| 107 | 'hide_empty' => $hide_empty, |
| 108 | ); |
| 109 | $list_args = array( |
| 110 | 'show_count' => $count, |
| 111 | 'hierarchical' => $hierarchical, |
| 112 | 'taxonomy' => 'product_cat', |
| 113 | 'hide_empty' => $hide_empty, |
| 114 | ); |
| 115 | $max_depth = absint( isset( $instance['max_depth'] ) ? $instance['max_depth'] : $this->settings['max_depth']['std'] ); |
| 116 | |
| 117 | $list_args['menu_order'] = false; |
| 118 | $dropdown_args['depth'] = $max_depth; |
| 119 | $list_args['depth'] = $max_depth; |
| 120 | |
| 121 | if ( 'order' === $orderby ) { |
| 122 | $list_args['orderby'] = 'meta_value_num'; |
| 123 | $dropdown_args['orderby'] = 'meta_value_num'; |
| 124 | $list_args['meta_key'] = 'order'; |
| 125 | $dropdown_args['meta_key'] = 'order'; |
| 126 | } |
| 127 | |
| 128 | $this->current_cat = false; |
| 129 | $this->cat_ancestors = array(); |
| 130 | |
| 131 | if ( is_tax( 'product_cat' ) ) { |
| 132 | $this->current_cat = $wp_query->queried_object; |
| 133 | $this->cat_ancestors = get_ancestors( $this->current_cat->term_id, 'product_cat' ); |
| 134 | |
| 135 | } elseif ( is_singular( 'product' ) ) { |
| 136 | $terms = wc_get_product_terms( |
| 137 | $post->ID, |
| 138 | 'product_cat', |
| 139 | apply_filters( |
| 140 | 'woocommerce_product_categories_widget_product_terms_args', |
| 141 | array( |
| 142 | 'orderby' => 'parent', |
| 143 | 'order' => 'DESC', |
| 144 | ) |
| 145 | ) |
| 146 | ); |
| 147 | |
| 148 | if ( $terms ) { |
| 149 | $main_term = apply_filters( 'woocommerce_product_categories_widget_main_term', $terms[0], $terms ); |
| 150 | $this->current_cat = $main_term; |
| 151 | $this->cat_ancestors = get_ancestors( $main_term->term_id, 'product_cat' ); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // Show Siblings and Children Only. |
| 156 | if ( $show_children_only && $this->current_cat ) { |
| 157 | if ( $hierarchical ) { |
| 158 | $include = array_merge( |
| 159 | $this->cat_ancestors, |
| 160 | array( $this->current_cat->term_id ), |
| 161 | get_terms( |
| 162 | 'product_cat', |
| 163 | array( |
| 164 | 'fields' => 'ids', |
| 165 | 'parent' => 0, |
| 166 | 'hierarchical' => true, |
| 167 | 'hide_empty' => false, |
| 168 | ) |
| 169 | ), |
| 170 | get_terms( |
| 171 | 'product_cat', |
| 172 | array( |
| 173 | 'fields' => 'ids', |
| 174 | 'parent' => $this->current_cat->term_id, |
| 175 | 'hierarchical' => true, |
| 176 | 'hide_empty' => false, |
| 177 | ) |
| 178 | ) |
| 179 | ); |
| 180 | // Gather siblings of ancestors. |
| 181 | if ( $this->cat_ancestors ) { |
| 182 | foreach ( $this->cat_ancestors as $ancestor ) { |
| 183 | $include = array_merge( |
| 184 | $include, |
| 185 | get_terms( |
| 186 | 'product_cat', |
| 187 | array( |
| 188 | 'fields' => 'ids', |
| 189 | 'parent' => $ancestor, |
| 190 | 'hierarchical' => false, |
| 191 | 'hide_empty' => false, |
| 192 | ) |
| 193 | ) |
| 194 | ); |
| 195 | } |
| 196 | } |
| 197 | } else { |
| 198 | // Direct children. |
| 199 | $include = get_terms( |
| 200 | 'product_cat', |
| 201 | array( |
| 202 | 'fields' => 'ids', |
| 203 | 'parent' => $this->current_cat->term_id, |
| 204 | 'hierarchical' => true, |
| 205 | 'hide_empty' => false, |
| 206 | ) |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | $list_args['include'] = implode( ',', $include ); |
| 211 | $dropdown_args['include'] = $list_args['include']; |
| 212 | |
| 213 | if ( empty( $include ) ) { |
| 214 | return; |
| 215 | } |
| 216 | } elseif ( $show_children_only ) { |
| 217 | $dropdown_args['depth'] = 1; |
| 218 | $dropdown_args['child_of'] = 0; |
| 219 | $dropdown_args['hierarchical'] = 1; |
| 220 | $list_args['depth'] = 1; |
| 221 | $list_args['child_of'] = 0; |
| 222 | $list_args['hierarchical'] = 1; |
| 223 | } |
| 224 | |
| 225 | $this->widget_start( $args, $instance ); |
| 226 | |
| 227 | if ( $dropdown ) { |
| 228 | wc_product_dropdown_categories( |
| 229 | apply_filters( |
| 230 | 'woocommerce_product_categories_widget_dropdown_args', |
| 231 | wp_parse_args( |
| 232 | $dropdown_args, |
| 233 | array( |
| 234 | 'show_count' => $count, |
| 235 | 'hierarchical' => $hierarchical, |
| 236 | 'show_uncategorized' => 0, |
| 237 | 'selected' => $this->current_cat ? $this->current_cat->slug : '', |
| 238 | ) |
| 239 | ) |
| 240 | ) |
| 241 | ); |
| 242 | |
| 243 | $handle = 'wc-product-category-dropdown-widget'; |
| 244 | wp_register_script( $handle, '', array( 'jquery', 'selectWoo' ), WC_VERSION, array( 'in_footer' => true ) ); |
| 245 | wp_enqueue_style( 'select2' ); |
| 246 | wp_enqueue_script( $handle ); |
| 247 | wp_add_inline_script( |
| 248 | $handle, |
| 249 | " |
| 250 | jQuery( '.dropdown_product_cat' ).on( 'change', function() { |
| 251 | const categoryValue = jQuery(this).val(); |
| 252 | |
| 253 | if ( categoryValue ) { |
| 254 | const homeUrl = '" . esc_js( home_url( '/' ) ) . "'; |
| 255 | const url = new URL( homeUrl, window.location.origin ); |
| 256 | url.searchParams.set( 'product_cat', categoryValue ); |
| 257 | location.href = url.toString(); |
| 258 | } else { |
| 259 | location.href = '" . esc_js( wc_get_page_permalink( 'shop' ) ) . "'; |
| 260 | } |
| 261 | }); |
| 262 | |
| 263 | if ( jQuery().selectWoo ) { |
| 264 | var wc_product_cat_select = function() { |
| 265 | jQuery( '.dropdown_product_cat' ).selectWoo( { |
| 266 | placeholder: '" . esc_js( __( 'Select a category', 'woocommerce' ) ) . "', |
| 267 | minimumResultsForSearch: 5, |
| 268 | width: '100%', |
| 269 | allowClear: true, |
| 270 | language: { |
| 271 | noResults: function() { |
| 272 | return '" . esc_js( _x( 'No matches found', 'enhanced select', 'woocommerce' ) ) . "'; |
| 273 | } |
| 274 | } |
| 275 | } ); |
| 276 | }; |
| 277 | wc_product_cat_select(); |
| 278 | } |
| 279 | " |
| 280 | ); |
| 281 | } else { |
| 282 | include_once WC()->plugin_path() . '/includes/walkers/class-wc-product-cat-list-walker.php'; |
| 283 | |
| 284 | $list_args['walker'] = new WC_Product_Cat_List_Walker(); |
| 285 | $list_args['title_li'] = ''; |
| 286 | $list_args['pad_counts'] = 1; |
| 287 | $list_args['show_option_none'] = __( 'No product categories exist.', 'woocommerce' ); |
| 288 | $list_args['current_category'] = ( $this->current_cat ) ? $this->current_cat->term_id : ''; |
| 289 | $list_args['current_category_ancestors'] = $this->cat_ancestors; |
| 290 | $list_args['max_depth'] = $max_depth; |
| 291 | |
| 292 | echo '<ul class="product-categories">'; |
| 293 | |
| 294 | wp_list_categories( apply_filters( 'woocommerce_product_categories_widget_args', $list_args ) ); |
| 295 | |
| 296 | echo '</ul>'; |
| 297 | } |
| 298 | |
| 299 | $this->widget_end( $args ); |
| 300 | } |
| 301 | } |
| 302 |