2011-08-10 17:11:11 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Product Search Widget
|
|
|
|
*
|
|
|
|
* @package WooCommerce
|
|
|
|
* @category Widgets
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
|
|
|
|
|
|
|
class WooCommerce_Widget_Product_Categories extends WP_Widget {
|
|
|
|
|
2011-08-26 21:28:55 +00:00
|
|
|
/** Variables to setup the widget. */
|
|
|
|
var $woo_widget_cssclass;
|
|
|
|
var $woo_widget_description;
|
|
|
|
var $woo_widget_idbase;
|
|
|
|
var $woo_widget_name;
|
|
|
|
|
2011-08-10 17:11:11 +00:00
|
|
|
/** constructor */
|
|
|
|
function WooCommerce_Widget_Product_Categories() {
|
2011-08-26 21:28:55 +00:00
|
|
|
|
|
|
|
/* Widget variable settings. */
|
|
|
|
$this->woo_widget_cssclass = 'widget_product_categories';
|
|
|
|
$this->woo_widget_description = __( 'A list or dropdown of product categories.', 'woothemes' );
|
|
|
|
$this->woo_widget_idbase = 'woocommerce_product_categories';
|
|
|
|
$this->woo_widget_name = __('WooCommerce Product Categories', 'woothemes' );
|
|
|
|
|
|
|
|
/* Widget settings. */
|
|
|
|
$widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description );
|
|
|
|
|
|
|
|
/* Create the widget. */
|
|
|
|
$this->WP_Widget('product_categories', $this->woo_widget_name, $widget_ops);
|
2011-08-10 17:11:11 +00:00
|
|
|
}
|
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
/** @see WP_Widget */
|
2011-08-10 17:11:11 +00:00
|
|
|
function widget( $args, $instance ) {
|
|
|
|
extract( $args );
|
|
|
|
|
|
|
|
$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Product Categories', 'woothemes' ) : $instance['title'], $instance, $this->id_base);
|
|
|
|
$c = $instance['count'] ? '1' : '0';
|
|
|
|
$h = $instance['hierarchical'] ? '1' : '0';
|
|
|
|
$d = $instance['dropdown'] ? '1' : '0';
|
|
|
|
|
|
|
|
echo $before_widget;
|
|
|
|
if ( $title )
|
|
|
|
echo $before_title . $title . $after_title;
|
|
|
|
|
|
|
|
$cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h, 'taxonomy' => 'product_cat');
|
|
|
|
|
|
|
|
if ( $d ) {
|
|
|
|
|
|
|
|
$terms = get_terms('product_cat');
|
|
|
|
$output = "<select name='product_cat' id='dropdown_product_cat'>";
|
|
|
|
$output .= '<option value="">'.__('Select Category', 'woothemes').'</option>';
|
|
|
|
foreach($terms as $term){
|
|
|
|
$term_slug=$term->slug;
|
|
|
|
$term_name =$term->name;
|
2011-09-13 14:07:10 +00:00
|
|
|
$output .="<option value='".$term_slug."'>".$term_name."</option>";
|
2011-08-10 17:11:11 +00:00
|
|
|
}
|
|
|
|
$output .="</select>";
|
|
|
|
echo $output;
|
|
|
|
|
|
|
|
?>
|
|
|
|
<script type='text/javascript'>
|
|
|
|
/* <![CDATA[ */
|
|
|
|
var dropdown = document.getElementById("dropdown_product_cat");
|
|
|
|
function onCatChange() {
|
|
|
|
if ( dropdown.options[dropdown.selectedIndex].value !=='' ) {
|
|
|
|
location.href = "<?php echo home_url(); ?>/?product_cat="+dropdown.options[dropdown.selectedIndex].value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dropdown.onchange = onCatChange;
|
|
|
|
/* ]]> */
|
|
|
|
</script>
|
|
|
|
<?php
|
|
|
|
|
|
|
|
} else {
|
|
|
|
?>
|
|
|
|
<ul>
|
|
|
|
<?php
|
|
|
|
$cat_args['title_li'] = '';
|
|
|
|
wp_list_categories(apply_filters('widget_product_categories_args', $cat_args));
|
|
|
|
?>
|
|
|
|
</ul>
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $after_widget;
|
|
|
|
}
|
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
/** @see WP_Widget->update */
|
2011-08-10 17:11:11 +00:00
|
|
|
function update( $new_instance, $old_instance ) {
|
|
|
|
$instance = $old_instance;
|
|
|
|
$instance['title'] = strip_tags($new_instance['title']);
|
|
|
|
$instance['count'] = !empty($new_instance['count']) ? 1 : 0;
|
|
|
|
$instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0;
|
|
|
|
$instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
/** @see WP_Widget->form */
|
2011-08-10 17:11:11 +00:00
|
|
|
function form( $instance ) {
|
|
|
|
//Defaults
|
|
|
|
$instance = wp_parse_args( (array) $instance, array( 'title' => '') );
|
|
|
|
$title = esc_attr( $instance['title'] );
|
|
|
|
$count = isset($instance['count']) ? (bool) $instance['count'] :false;
|
|
|
|
$hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
|
|
|
|
$dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
|
|
|
|
?>
|
|
|
|
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', 'woothemes' ); ?></label>
|
2011-09-19 06:01:26 +00:00
|
|
|
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('title') ); ?>" name="<?php echo esc_attr( $this->get_field_name('title') ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
|
2011-08-10 17:11:11 +00:00
|
|
|
|
2011-09-19 06:01:26 +00:00
|
|
|
<p><input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id('dropdown') ); ?>" name="<?php echo esc_attr( $this->get_field_name('dropdown') ); ?>"<?php checked( $dropdown ); ?> />
|
2011-08-10 17:11:11 +00:00
|
|
|
<label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Show as dropdown', 'woothemes' ); ?></label><br />
|
|
|
|
|
2011-09-19 06:01:26 +00:00
|
|
|
<input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id('count') ); ?>" name="<?php echo esc_attr( $this->get_field_name('count') ); ?>"<?php checked( $count ); ?> />
|
2011-08-10 17:11:11 +00:00
|
|
|
<label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts', 'woothemes' ); ?></label><br />
|
|
|
|
|
2011-09-19 06:01:26 +00:00
|
|
|
<input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id('hierarchical') ); ?>" name="<?php echo esc_attr( $this->get_field_name('hierarchical') ); ?>"<?php checked( $hierarchical ); ?> />
|
2011-08-10 17:11:11 +00:00
|
|
|
<label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy', 'woothemes' ); ?></label></p>
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
|
|
|
|
} // class WooCommerce_Widget_Product_Categories
|