2012-05-26 16:25:07 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2013-02-12 10:51:42 +00:00
|
|
|
* Product Categories Widget
|
2012-08-14 17:37:50 +00:00
|
|
|
*
|
|
|
|
* @author WooThemes
|
|
|
|
* @category Widgets
|
|
|
|
* @package WooCommerce/Widgets
|
2013-05-24 15:51:58 +00:00
|
|
|
* @version 2.1.0
|
|
|
|
* @extends WC_Widget
|
2012-05-26 16:25:07 +00:00
|
|
|
*/
|
2012-10-15 10:57:58 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
class WC_Widget_Product_Categories extends WC_Widget {
|
2012-05-26 16:25:07 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
public $cat_ancestors;
|
|
|
|
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',
|
|
|
|
'std' => __( 'Top Rated Products', 'woocommerce' ),
|
|
|
|
'label' => __( 'Title', 'woocommerce' )
|
|
|
|
),
|
|
|
|
'orderby' => array(
|
|
|
|
'type' => 'select',
|
|
|
|
'std' => 'name',
|
|
|
|
'label' => __( 'Title', '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 for the current category', 'woocommerce' )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
parent::__construct();
|
2012-05-26 16:25:07 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 17:37:50 +00:00
|
|
|
/**
|
|
|
|
* widget function.
|
|
|
|
*
|
|
|
|
* @see WP_Widget
|
|
|
|
* @access public
|
|
|
|
* @param array $args
|
|
|
|
* @param array $instance
|
|
|
|
* @return void
|
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
public function widget( $args, $instance ) {
|
2012-05-26 16:25:07 +00:00
|
|
|
extract( $args );
|
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
|
|
|
|
$c = $instance['count'] ? '1' : '0';
|
|
|
|
$h = $instance['hierarchical'] ? true : false;
|
|
|
|
$s = $instance['show_children_only'] ? '1' : '0';
|
|
|
|
$d = $instance['dropdown'] ? '1' : '0';
|
|
|
|
$o = $instance['orderby'] ? $instance['orderby'] : 'order';
|
2012-05-26 16:25:07 +00:00
|
|
|
|
|
|
|
echo $before_widget;
|
2013-05-24 15:51:58 +00:00
|
|
|
|
|
|
|
if ( $title )
|
|
|
|
echo $before_title . $title . $after_title;
|
2012-05-26 16:25:07 +00:00
|
|
|
|
2013-01-14 11:56:50 +00:00
|
|
|
$cat_args = array( 'show_count' => $c, 'hierarchical' => $h, 'taxonomy' => 'product_cat' );
|
|
|
|
|
|
|
|
$cat_args['menu_order'] = false;
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
if ( $o == 'order' ) {
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
$cat_args['menu_order'] = 'asc';
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
} else {
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2012-06-10 08:11:33 +00:00
|
|
|
$cat_args['orderby'] = 'title';
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
if ( $d ) {
|
|
|
|
|
|
|
|
// Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
|
2013-01-14 11:56:50 +00:00
|
|
|
woocommerce_product_dropdown_categories( $c, $h, 0, $o );
|
2012-05-26 16:25:07 +00:00
|
|
|
?>
|
|
|
|
<script type='text/javascript'>
|
|
|
|
/* <![CDATA[ */
|
2012-09-14 15:33:07 +00:00
|
|
|
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;
|
2012-05-26 16:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-14 15:33:07 +00:00
|
|
|
product_cat_dropdown.onchange = onProductCatChange;
|
2012-05-26 16:25:07 +00:00
|
|
|
/* ]]> */
|
|
|
|
</script>
|
|
|
|
<?php
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
} else {
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
global $wp_query, $post, $woocommerce;
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
$this->current_cat = false;
|
|
|
|
$this->cat_ancestors = array();
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2013-02-27 13:43:38 +00:00
|
|
|
if ( is_tax('product_cat') ) {
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
$this->current_cat = $wp_query->queried_object;
|
|
|
|
$this->cat_ancestors = get_ancestors( $this->current_cat->term_id, 'product_cat' );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2013-02-27 13:43:38 +00:00
|
|
|
} elseif ( is_singular('product') ) {
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2013-02-27 13:43:38 +00:00
|
|
|
$product_category = wp_get_post_terms( $post->ID, 'product_cat', array( 'orderby' => 'parent' ) );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2013-02-27 13:43:38 +00:00
|
|
|
if ( $product_category ) {
|
|
|
|
$this->current_cat = end( $product_category );
|
2012-05-26 16:25:07 +00:00
|
|
|
$this->cat_ancestors = get_ancestors( $this->current_cat->term_id, 'product_cat' );
|
2013-02-27 13:43:38 +00:00
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2013-02-27 13:43:38 +00:00
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2013-06-11 13:28:45 +00:00
|
|
|
include_once( $woocommerce->plugin_path() . '/includes/walkers/class-product-cat-list-walker.php' );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
$cat_args['walker'] = new WC_Product_Cat_List_Walker;
|
|
|
|
$cat_args['title_li'] = '';
|
|
|
|
$cat_args['show_children_only'] = ( isset( $instance['show_children_only'] ) && $instance['show_children_only'] ) ? 1 : 0;
|
|
|
|
$cat_args['pad_counts'] = 1;
|
2012-10-16 09:45:33 +00:00
|
|
|
$cat_args['show_option_none'] = __('No product categories exist.', 'woocommerce' );
|
2012-05-26 16:25:07 +00:00
|
|
|
$cat_args['current_category'] = ( $this->current_cat ) ? $this->current_cat->term_id : '';
|
|
|
|
$cat_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
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
wp_list_categories( apply_filters( 'woocommerce_product_categories_widget_args', $cat_args ) );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
echo '</ul>';
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $after_widget;
|
|
|
|
}
|
2013-02-12 10:51:42 +00:00
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
|
|
|
|
register_widget( 'WC_Widget_Product_Categories' );
|