2012-05-26 16:25:07 +00:00
|
|
|
<?php
|
2014-11-19 17:12:56 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
/**
|
2013-02-12 10:51:42 +00:00
|
|
|
* Product Categories Widget
|
2012-08-14 17:37:50 +00:00
|
|
|
*
|
2014-11-15 01:12:59 +00:00
|
|
|
* @author WooThemes
|
|
|
|
* @category Widgets
|
|
|
|
* @package WooCommerce/Widgets
|
|
|
|
* @version 2.3.0
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* Category ancestors
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
public $cat_ancestors;
|
2014-11-15 01:12:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current Category
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
public $current_cat;
|
2012-08-14 17:37:50 +00:00
|
|
|
|
|
|
|
/**
|
2013-05-24 15:51:58 +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';
|
|
|
|
$this->widget_name = __( 'WooCommerce Product Categories', 'woocommerce' );
|
|
|
|
$this->settings = array(
|
|
|
|
'title' => array(
|
|
|
|
'type' => 'text',
|
2013-06-28 09:07:18 +00:00
|
|
|
'std' => __( 'Product Categories', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
'label' => __( 'Title', 'woocommerce' )
|
|
|
|
),
|
|
|
|
'orderby' => array(
|
|
|
|
'type' => 'select',
|
|
|
|
'std' => 'name',
|
2013-10-31 21:42:31 +00:00
|
|
|
'label' => __( 'Order by', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
'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,
|
2014-04-07 11:59:38 +00:00
|
|
|
'label' => __( 'Only show children of the current category', '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
|
|
|
/**
|
|
|
|
* widget function.
|
|
|
|
*
|
|
|
|
* @see WP_Widget
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
2012-08-14 17:37:50 +00:00
|
|
|
* @param array $args
|
|
|
|
* @param array $instance
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
2012-08-14 17:37:50 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
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
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
$c = isset( $instance['count'] ) ? $instance['count'] : $this->settings['count']['std'];
|
|
|
|
$h = isset( $instance['hierarchical'] ) ? $instance['hierarchical'] : $this->settings['hierarchical']['std'];
|
|
|
|
$s = isset( $instance['show_children_only'] ) ? $instance['show_children_only'] : $this->settings['show_children_only']['std'];
|
|
|
|
$d = isset( $instance['dropdown'] ) ? $instance['dropdown'] : $this->settings['dropdown']['std'];
|
|
|
|
$o = isset( $instance['orderby'] ) ? $instance['orderby'] : $this->settings['orderby']['std'];
|
2014-03-17 16:52:24 +00:00
|
|
|
$dropdown_args = array( 'hide_empty' => false );
|
|
|
|
$list_args = array( 'show_count' => $c, 'hierarchical' => $h, 'taxonomy' => 'product_cat', 'hide_empty' => false );
|
2013-01-14 11:56:50 +00:00
|
|
|
|
2014-02-17 21:00:25 +00:00
|
|
|
// Menu Order
|
2014-03-17 16:52:24 +00:00
|
|
|
$list_args['menu_order'] = false;
|
2012-05-26 16:25:07 +00:00
|
|
|
if ( $o == 'order' ) {
|
2014-03-17 16:52:24 +00:00
|
|
|
$list_args['menu_order'] = 'asc';
|
2012-05-26 16:25:07 +00:00
|
|
|
} else {
|
2014-03-17 16:52:24 +00:00
|
|
|
$list_args['orderby'] = 'title';
|
2014-02-17 21:00:25 +00:00
|
|
|
}
|
2014-09-20 19:37:45 +00:00
|
|
|
|
2014-02-17 21:00:25 +00:00
|
|
|
// Setup Current Category
|
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-02-17 21:00:25 +00:00
|
|
|
|
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' ) ) {
|
2014-02-17 21:00:25 +00:00
|
|
|
|
|
|
|
$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' );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-09-20 19:37:45 +00:00
|
|
|
|
2014-02-17 21:00:25 +00:00
|
|
|
// Show Siblings and Children Only
|
2014-02-17 21:09:58 +00:00
|
|
|
if ( $s && $this->current_cat ) {
|
2014-03-04 11:47:17 +00:00
|
|
|
|
|
|
|
// Top level is needed
|
2014-09-20 19:37:45 +00:00
|
|
|
$top_level = get_terms(
|
|
|
|
'product_cat',
|
|
|
|
array(
|
|
|
|
'fields' => 'ids',
|
|
|
|
'parent' => 0,
|
|
|
|
'hierarchical' => true,
|
2014-03-17 16:52:24 +00:00
|
|
|
'hide_empty' => false
|
2014-09-20 19:37:45 +00:00
|
|
|
)
|
2014-03-17 16:52:24 +00:00
|
|
|
);
|
2014-03-04 11:47:17 +00:00
|
|
|
|
|
|
|
// Direct children are wanted
|
2014-09-20 19:37:45 +00:00
|
|
|
$direct_children = get_terms(
|
|
|
|
'product_cat',
|
|
|
|
array(
|
|
|
|
'fields' => 'ids',
|
|
|
|
'parent' => $this->current_cat->term_id,
|
|
|
|
'hierarchical' => true,
|
|
|
|
'hide_empty' => false
|
|
|
|
)
|
2014-03-17 16:52:24 +00:00
|
|
|
);
|
2014-09-20 19:37:45 +00:00
|
|
|
|
2014-03-04 11:47:17 +00:00
|
|
|
// Gather siblings of ancestors
|
|
|
|
$siblings = array();
|
|
|
|
if ( $this->cat_ancestors ) {
|
|
|
|
foreach ( $this->cat_ancestors as $ancestor ) {
|
2014-09-20 19:37:45 +00:00
|
|
|
$ancestor_siblings = get_terms(
|
|
|
|
'product_cat',
|
|
|
|
array(
|
|
|
|
'fields' => 'ids',
|
|
|
|
'parent' => $ancestor,
|
|
|
|
'hierarchical' => false,
|
|
|
|
'hide_empty' => false
|
2014-03-17 16:52:24 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$siblings = array_merge( $siblings, $ancestor_siblings );
|
2014-03-04 11:47:17 +00:00
|
|
|
}
|
2014-02-17 21:00:25 +00:00
|
|
|
}
|
2014-03-04 11:47:17 +00:00
|
|
|
|
2014-04-07 11:59:38 +00:00
|
|
|
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 );
|
|
|
|
}
|
2014-09-20 19:37:45 +00:00
|
|
|
|
2014-03-04 11:47:17 +00:00
|
|
|
$dropdown_args['include'] = implode( ',', $include );
|
2014-03-17 16:52:24 +00:00
|
|
|
$list_args['include'] = implode( ',', $include );
|
2014-04-07 11:59:38 +00:00
|
|
|
|
|
|
|
if ( empty( $include ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-20 19:37:45 +00:00
|
|
|
|
2014-03-04 11:47:17 +00:00
|
|
|
} elseif ( $s ) {
|
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;
|
|
|
|
}
|
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
$this->widget_start( $args, $instance );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2014-02-17 21:00:25 +00:00
|
|
|
// Dropdown
|
2012-05-26 16:25:07 +00:00
|
|
|
if ( $d ) {
|
2014-02-17 21:00:25 +00:00
|
|
|
$dropdown_defaults = array(
|
2013-08-20 12:32:38 +00:00
|
|
|
'show_counts' => $c,
|
|
|
|
'hierarchical' => $h,
|
|
|
|
'show_uncategorized' => 0,
|
2014-03-04 11:47:17 +00:00
|
|
|
'orderby' => $o,
|
|
|
|
'selected' => $this->current_cat ? $this->current_cat->slug : ''
|
2014-02-17 21:00:25 +00:00
|
|
|
);
|
|
|
|
$dropdown_args = wp_parse_args( $dropdown_args, $dropdown_defaults );
|
|
|
|
|
|
|
|
// Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
|
2014-05-28 10:28:20 +00:00
|
|
|
wc_product_dropdown_categories( apply_filters( 'woocommerce_product_categories_widget_dropdown_args', $dropdown_args ) );
|
2014-09-20 19:37:45 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
wc_enqueue_js( "
|
2014-08-22 19:39:10 +00:00
|
|
|
jQuery('.dropdown_product_cat').change(function(){
|
2014-08-22 21:01:25 +00:00
|
|
|
if(jQuery(this).val() != '') {
|
2014-08-22 19:39:10 +00:00
|
|
|
location.href = '" . home_url() . "/?product_cat=' + jQuery(this).val();
|
2014-08-15 12:47:37 +00:00
|
|
|
}
|
2014-08-22 19:39:10 +00:00
|
|
|
});
|
2014-11-15 01:12:59 +00:00
|
|
|
" );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2014-02-17 21:00:25 +00:00
|
|
|
// List
|
2012-05-26 16:25:07 +00:00
|
|
|
} else {
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2013-11-25 14:01:32 +00:00
|
|
|
include_once( WC()->plugin_path() . '/includes/walkers/class-product-cat-list-walker.php' );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2014-03-17 16:52:24 +00:00
|
|
|
$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;
|
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>';
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|