2012-05-26 16:25:07 +00:00
|
|
|
<?php
|
2017-08-28 19:36:32 +00:00
|
|
|
/**
|
|
|
|
* Product Categories Widget
|
|
|
|
*
|
2018-03-09 20:28:08 +00:00
|
|
|
* @package WooCommerce/Widgets
|
|
|
|
* @version 2.3.0
|
2017-08-28 19:36:32 +00:00
|
|
|
*/
|
2014-11-19 17:12:56 +00:00
|
|
|
|
2018-03-09 20:28:08 +00:00
|
|
|
defined( 'ABSPATH' ) || exit;
|
2014-11-19 17:12:56 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
/**
|
2017-08-28 19:36:32 +00:00
|
|
|
* Product categories widget class.
|
2012-08-14 17:37:50 +00:00
|
|
|
*
|
2017-08-28 19:36:32 +00:00
|
|
|
* @extends WC_Widget
|
2012-05-26 16:25:07 +00:00
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
class WC_Widget_Product_Categories extends WC_Widget {
|
2012-05-26 16:25:07 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Category ancestors.
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
public $cat_ancestors;
|
2014-11-15 01:12:59 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Current Category.
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
public $current_cat;
|
2012-08-14 17:37:50 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Constructor.
|
2012-08-14 17:37:50 +00:00
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
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';
|
2017-08-25 11:07:17 +00:00
|
|
|
$this->widget_name = __( 'Product Categories', 'woocommerce' );
|
2013-05-24 15:51:58 +00:00
|
|
|
$this->settings = array(
|
2018-03-09 20:28:08 +00:00
|
|
|
'title' => array(
|
2013-05-24 15:51:58 +00:00
|
|
|
'type' => 'text',
|
2016-10-12 10:16:30 +00:00
|
|
|
'std' => __( 'Product categories', 'woocommerce' ),
|
2016-08-27 01:46:45 +00:00
|
|
|
'label' => __( 'Title', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
),
|
2018-03-09 20:28:08 +00:00
|
|
|
'orderby' => array(
|
|
|
|
'type' => 'select',
|
|
|
|
'std' => 'name',
|
|
|
|
'label' => __( 'Order by', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
'options' => array(
|
2016-10-12 10:16:30 +00:00
|
|
|
'order' => __( 'Category order', 'woocommerce' ),
|
2016-08-27 01:46:45 +00:00
|
|
|
'name' => __( 'Name', 'woocommerce' ),
|
|
|
|
),
|
2013-05-24 15:51:58 +00:00
|
|
|
),
|
2018-03-09 20:28:08 +00:00
|
|
|
'dropdown' => array(
|
2013-05-24 15:51:58 +00:00
|
|
|
'type' => 'checkbox',
|
|
|
|
'std' => 0,
|
2016-08-27 01:46:45 +00:00
|
|
|
'label' => __( 'Show as dropdown', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
),
|
2018-03-09 20:28:08 +00:00
|
|
|
'count' => array(
|
2013-05-24 15:51:58 +00:00
|
|
|
'type' => 'checkbox',
|
|
|
|
'std' => 0,
|
2016-08-27 01:46:45 +00:00
|
|
|
'label' => __( 'Show product counts', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
),
|
2018-03-09 20:28:08 +00:00
|
|
|
'hierarchical' => array(
|
2013-05-24 15:51:58 +00:00
|
|
|
'type' => 'checkbox',
|
|
|
|
'std' => 1,
|
2016-08-27 01:46:45 +00:00
|
|
|
'label' => __( 'Show hierarchy', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
),
|
|
|
|
'show_children_only' => array(
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'std' => 0,
|
2016-08-27 01:46:45 +00:00
|
|
|
'label' => __( 'Only show children of the current category', 'woocommerce' ),
|
2015-10-27 16:10:32 +00:00
|
|
|
),
|
2018-03-09 20:28:08 +00:00
|
|
|
'hide_empty' => array(
|
2015-10-27 16:10:32 +00:00
|
|
|
'type' => 'checkbox',
|
|
|
|
'std' => 0,
|
2016-08-27 01:46:45 +00:00
|
|
|
'label' => __( 'Hide empty categories', 'woocommerce' ),
|
|
|
|
),
|
2018-03-09 20:28:08 +00:00
|
|
|
'max_depth' => array(
|
2017-08-25 11:49:40 +00:00
|
|
|
'type' => 'text',
|
|
|
|
'std' => '',
|
|
|
|
'label' => __( 'Maximum depth', 'woocommerce' ),
|
|
|
|
),
|
2013-05-24 15:51:58 +00:00
|
|
|
);
|
2014-11-15 01:12:59 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
parent::__construct();
|
2012-05-26 16:25:07 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 17:37:50 +00:00
|
|
|
/**
|
2016-01-06 18:58:38 +00:00
|
|
|
* Output widget.
|
2012-08-14 17:37:50 +00:00
|
|
|
*
|
|
|
|
* @see WP_Widget
|
2017-08-28 19:36:32 +00:00
|
|
|
* @param array $args Widget arguments.
|
|
|
|
* @param array $instance Widget instance.
|
2012-08-14 17:37:50 +00:00
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
public function widget( $args, $instance ) {
|
2014-06-08 20:33:11 +00:00
|
|
|
global $wp_query, $post;
|
2014-02-17 21:00:25 +00:00
|
|
|
|
2015-10-27 16:10:32 +00:00
|
|
|
$count = isset( $instance['count'] ) ? $instance['count'] : $this->settings['count']['std'];
|
|
|
|
$hierarchical = isset( $instance['hierarchical'] ) ? $instance['hierarchical'] : $this->settings['hierarchical']['std'];
|
|
|
|
$show_children_only = isset( $instance['show_children_only'] ) ? $instance['show_children_only'] : $this->settings['show_children_only']['std'];
|
|
|
|
$dropdown = isset( $instance['dropdown'] ) ? $instance['dropdown'] : $this->settings['dropdown']['std'];
|
|
|
|
$orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : $this->settings['orderby']['std'];
|
|
|
|
$hide_empty = isset( $instance['hide_empty'] ) ? $instance['hide_empty'] : $this->settings['hide_empty']['std'];
|
2017-08-28 19:36:32 +00:00
|
|
|
$dropdown_args = array(
|
|
|
|
'hide_empty' => $hide_empty,
|
|
|
|
);
|
|
|
|
$list_args = array(
|
|
|
|
'show_count' => $count,
|
|
|
|
'hierarchical' => $hierarchical,
|
|
|
|
'taxonomy' => 'product_cat',
|
|
|
|
'hide_empty' => $hide_empty,
|
|
|
|
);
|
|
|
|
$max_depth = absint( isset( $instance['max_depth'] ) ? $instance['max_depth'] : $this->settings['max_depth']['std'] );
|
2013-01-14 11:56:50 +00:00
|
|
|
|
2014-03-17 16:52:24 +00:00
|
|
|
$list_args['menu_order'] = false;
|
2017-08-25 11:49:40 +00:00
|
|
|
$dropdown_args['depth'] = $max_depth;
|
|
|
|
$list_args['depth'] = $max_depth;
|
|
|
|
|
2016-09-09 00:14:28 +00:00
|
|
|
if ( 'order' === $orderby ) {
|
2014-03-17 16:52:24 +00:00
|
|
|
$list_args['menu_order'] = 'asc';
|
2012-05-26 16:25:07 +00:00
|
|
|
} else {
|
2018-03-09 20:28:08 +00:00
|
|
|
$list_args['orderby'] = 'title';
|
2014-02-17 21:00:25 +00:00
|
|
|
}
|
2014-09-20 19:37:45 +00:00
|
|
|
|
2014-03-04 11:47:17 +00:00
|
|
|
$this->current_cat = false;
|
2014-02-17 21:00:25 +00:00
|
|
|
$this->cat_ancestors = array();
|
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
if ( is_tax( 'product_cat' ) ) {
|
2014-03-04 11:47:17 +00:00
|
|
|
$this->current_cat = $wp_query->queried_object;
|
2014-02-17 21:00:25 +00:00
|
|
|
$this->cat_ancestors = get_ancestors( $this->current_cat->term_id, 'product_cat' );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
} elseif ( is_singular( 'product' ) ) {
|
2018-03-09 20:28:08 +00:00
|
|
|
$terms = wc_get_product_terms(
|
|
|
|
$post->ID, 'product_cat', apply_filters(
|
|
|
|
'woocommerce_product_categories_widget_product_terms_args', array(
|
|
|
|
'orderby' => 'parent',
|
|
|
|
'order' => 'DESC',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2014-02-17 21:00:25 +00:00
|
|
|
|
2018-02-20 11:49:06 +00:00
|
|
|
if ( $terms ) {
|
2018-03-09 20:28:08 +00:00
|
|
|
$main_term = apply_filters( 'woocommerce_product_categories_widget_main_term', $terms[0], $terms );
|
2018-02-20 11:49:06 +00:00
|
|
|
$this->current_cat = $main_term;
|
|
|
|
$this->cat_ancestors = get_ancestors( $main_term->term_id, 'product_cat' );
|
2014-02-17 21:00:25 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-20 19:37:45 +00:00
|
|
|
|
2017-08-25 11:49:40 +00:00
|
|
|
// Show Siblings and Children Only.
|
2015-10-27 16:10:32 +00:00
|
|
|
if ( $show_children_only && $this->current_cat ) {
|
2017-08-25 11:49:40 +00:00
|
|
|
if ( $hierarchical ) {
|
|
|
|
$include = array_merge(
|
|
|
|
$this->cat_ancestors,
|
|
|
|
array( $this->current_cat->term_id ),
|
|
|
|
get_terms(
|
2014-09-20 19:37:45 +00:00
|
|
|
'product_cat',
|
|
|
|
array(
|
|
|
|
'fields' => 'ids',
|
2017-08-25 11:49:40 +00:00
|
|
|
'parent' => 0,
|
|
|
|
'hierarchical' => true,
|
2016-08-27 01:46:45 +00:00
|
|
|
'hide_empty' => false,
|
2014-03-17 16:52:24 +00:00
|
|
|
)
|
2017-08-25 11:49:40 +00:00
|
|
|
),
|
|
|
|
get_terms(
|
|
|
|
'product_cat',
|
|
|
|
array(
|
|
|
|
'fields' => 'ids',
|
|
|
|
'parent' => $this->current_cat->term_id,
|
|
|
|
'hierarchical' => true,
|
|
|
|
'hide_empty' => false,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
// Gather siblings of ancestors.
|
|
|
|
if ( $this->cat_ancestors ) {
|
|
|
|
foreach ( $this->cat_ancestors as $ancestor ) {
|
2018-03-09 20:28:08 +00:00
|
|
|
$include = array_merge(
|
|
|
|
$include, get_terms(
|
|
|
|
'product_cat',
|
|
|
|
array(
|
|
|
|
'fields' => 'ids',
|
|
|
|
'parent' => $ancestor,
|
|
|
|
'hierarchical' => false,
|
|
|
|
'hide_empty' => false,
|
|
|
|
)
|
2017-08-25 11:49:40 +00:00
|
|
|
)
|
2018-03-09 20:28:08 +00:00
|
|
|
);
|
2017-08-25 11:49:40 +00:00
|
|
|
}
|
2014-03-04 11:47:17 +00:00
|
|
|
}
|
2014-04-07 11:59:38 +00:00
|
|
|
} else {
|
2017-08-25 11:49:40 +00:00
|
|
|
// Direct children.
|
|
|
|
$include = get_terms(
|
|
|
|
'product_cat',
|
|
|
|
array(
|
|
|
|
'fields' => 'ids',
|
|
|
|
'parent' => $this->current_cat->term_id,
|
|
|
|
'hierarchical' => true,
|
|
|
|
'hide_empty' => false,
|
|
|
|
)
|
|
|
|
);
|
2018-03-09 20:28:08 +00:00
|
|
|
}
|
2014-09-20 19:37:45 +00:00
|
|
|
|
2017-08-28 19:36:32 +00:00
|
|
|
$list_args['include'] = implode( ',', $include );
|
|
|
|
$dropdown_args['include'] = $list_args['include'];
|
2014-04-07 11:59:38 +00:00
|
|
|
|
|
|
|
if ( empty( $include ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-27 16:10:32 +00:00
|
|
|
} elseif ( $show_children_only ) {
|
2014-04-07 11:59:38 +00:00
|
|
|
$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;
|
2018-03-09 20:28:08 +00:00
|
|
|
}
|
2014-04-07 11:59:38 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
$this->widget_start( $args, $instance );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2015-10-27 16:10:32 +00:00
|
|
|
if ( $dropdown ) {
|
2018-03-09 20:28:08 +00:00
|
|
|
wc_product_dropdown_categories(
|
|
|
|
apply_filters(
|
|
|
|
'woocommerce_product_categories_widget_dropdown_args', wp_parse_args(
|
|
|
|
$dropdown_args, array(
|
|
|
|
'show_count' => $count,
|
|
|
|
'hierarchical' => $hierarchical,
|
|
|
|
'show_uncategorized' => 0,
|
|
|
|
'orderby' => $orderby,
|
|
|
|
'selected' => $this->current_cat ? $this->current_cat->slug : '',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2018-06-25 13:58:18 +00:00
|
|
|
|
|
|
|
wp_enqueue_script( 'selectWoo' );
|
|
|
|
wp_enqueue_style( 'select2' );
|
|
|
|
|
2018-03-09 20:28:08 +00:00
|
|
|
wc_enqueue_js(
|
|
|
|
"
|
2015-06-22 09:22:15 +00:00
|
|
|
jQuery( '.dropdown_product_cat' ).change( function() {
|
|
|
|
if ( jQuery(this).val() != '' ) {
|
2015-09-02 11:23:53 +00:00
|
|
|
var this_page = '';
|
2015-06-22 09:22:15 +00:00
|
|
|
var home_url = '" . esc_js( home_url( '/' ) ) . "';
|
2015-09-02 11:23:53 +00:00
|
|
|
if ( home_url.indexOf( '?' ) > 0 ) {
|
2015-06-22 09:22:15 +00:00
|
|
|
this_page = home_url + '&product_cat=' + jQuery(this).val();
|
|
|
|
} else {
|
|
|
|
this_page = home_url + '?product_cat=' + jQuery(this).val();
|
|
|
|
}
|
|
|
|
location.href = this_page;
|
2018-06-26 09:54:04 +00:00
|
|
|
} else {
|
|
|
|
location.href = '" . esc_js( wc_get_page_permalink( 'shop' ) ) . "';
|
2014-08-15 12:47:37 +00:00
|
|
|
}
|
2014-08-22 19:39:10 +00:00
|
|
|
});
|
2018-06-25 13:58:18 +00:00
|
|
|
|
|
|
|
if ( jQuery().selectWoo ) {
|
|
|
|
var wc_product_cat_select = function() {
|
|
|
|
jQuery( '.dropdown_product_cat' ).selectWoo( {
|
|
|
|
placeholder: '" . esc_js( __( 'Select a category', 'woocommerce' ) ) . "',
|
|
|
|
minimumResultsForSearch: 5,
|
|
|
|
width: '100%',
|
|
|
|
allowClear: true,
|
|
|
|
language: {
|
|
|
|
noResults: function() {
|
|
|
|
return '" . esc_js( _x( 'No matches found', 'enhanced select', 'woocommerce' ) ) . "';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
wc_product_cat_select();
|
|
|
|
}
|
2018-03-09 20:28:08 +00:00
|
|
|
"
|
|
|
|
);
|
2012-05-26 16:25:07 +00:00
|
|
|
} else {
|
2018-03-12 15:31:57 +00:00
|
|
|
include_once WC()->plugin_path() . '/includes/walkers/class-wc-product-cat-list-walker.php';
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2018-03-09 20:28:08 +00:00
|
|
|
$list_args['walker'] = new WC_Product_Cat_List_Walker();
|
2014-03-17 16:52:24 +00:00
|
|
|
$list_args['title_li'] = '';
|
|
|
|
$list_args['pad_counts'] = 1;
|
2016-09-02 01:51:31 +00:00
|
|
|
$list_args['show_option_none'] = __( 'No product categories exist.', 'woocommerce' );
|
2017-08-28 19:36:32 +00:00
|
|
|
$list_args['current_category'] = ( $this->current_cat ) ? $this->current_cat->term_id : '';
|
2014-03-17 16:52:24 +00:00
|
|
|
$list_args['current_category_ancestors'] = $this->cat_ancestors;
|
2017-10-13 11:50:49 +00:00
|
|
|
$list_args['max_depth'] = $max_depth;
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
echo '<ul class="product-categories">';
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2014-03-17 16:52:24 +00:00
|
|
|
wp_list_categories( apply_filters( 'woocommerce_product_categories_widget_args', $list_args ) );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
echo '</ul>';
|
2018-03-09 20:28:08 +00:00
|
|
|
}
|
2012-05-26 16:25:07 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
$this->widget_end( $args );
|
2012-05-26 16:25:07 +00:00
|
|
|
}
|
2013-02-12 10:51:42 +00:00
|
|
|
}
|