/** @see WP_Widget */
    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';
        $o = isset($instance['orderby']) ? $instance['orderby'] : 'order';
        echo $before_widget;
        if ($title) {
            echo $before_title . $title . $after_title;
        }
        $cat_args = array('show_count' => $c, 'hierarchical' => $h, 'taxonomy' => 'product_cat');
        if ($o == 'order') {
            $cat_args['menu_order'] = 'asc';
        } else {
            $cat_args['orderby'] = $o;
        }
        if ($d) {
            // Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
            woocommerce_product_dropdown_categories($c, $h);
            ?>
			<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('woocommerce_product_categories_widget_args', $cat_args));
            ?>
		</ul>
<?php 
        }
        echo $after_widget;
    }
    /** @see WP_Widget */
    function widget($args, $instance)
    {
        extract($args);
        $title = apply_filters('widget_title', empty($instance['title']) ? __('Product Categories', 'woocommerce') : $instance['title'], $instance, $this->id_base);
        $c = $instance['count'] ? '1' : '0';
        $h = $instance['hierarchical'] ? true : false;
        $s = isset($instance['show_children_only']) && $instance['show_children_only'] ? '1' : '0';
        $d = $instance['dropdown'] ? '1' : '0';
        $o = isset($instance['orderby']) ? $instance['orderby'] : 'order';
        echo $before_widget;
        if ($title) {
            echo $before_title . $title . $after_title;
        }
        $cat_args = array('show_count' => $c, 'hierarchical' => $h, 'taxonomy' => 'product_cat');
        if ($o == 'order') {
            $cat_args['menu_order'] = 'asc';
        } else {
            $cat_args['orderby'] = $o;
        }
        if ($d) {
            // Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
            woocommerce_product_dropdown_categories($c, $h);
            ?>
			<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 {
            global $wp_query, $post, $woocommerce;
            $this->current_cat = false;
            $this->cat_ancestors = array();
            if (is_tax('product_cat')) {
                $this->current_cat = $wp_query->queried_object;
                $this->cat_ancestors = get_ancestors($this->current_cat->term_id, 'product_cat');
            } elseif (is_singular('product')) {
                $product_category = wp_get_post_terms($post->ID, 'product_cat');
                if ($product_category) {
                    $this->current_cat = end($product_category);
                    $this->cat_ancestors = get_ancestors($this->current_cat->term_id, 'product_cat');
                }
            }
            include_once $woocommerce->plugin_path() . '/classes/walkers/class-product-cat-list-walker.php';
            $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;
            $cat_args['show_option_none'] = __('No product categories exist.', 'woocommerce');
            $cat_args['current_category'] = $this->current_cat ? $this->current_cat->term_id : '';
            $cat_args['current_category_ancestors'] = $this->cat_ancestors;
            echo '<ul class="product-categories">';
            wp_list_categories(apply_filters('woocommerce_product_categories_widget_args', $cat_args));
            echo '</ul>';
        }
        echo $after_widget;
    }
Example #3
0
/**
 * Filter products by category, uses slugs for option values.
 *
 * Code adapted by Andrew Benbow
 *
 * @access public
 * @return void
 */
function woocommerce_products_by_category()
{
    global $typenow, $wp_query;
    if ($typenow == 'product') {
        woocommerce_product_dropdown_categories();
    }
}
    /** @see WP_Widget */
    function widget($args, $instance)
    {
        extract($args);
        $title = apply_filters('widget_title', empty($instance['title']) ? __('Product Categories', 'woocommerce') : $instance['title'], $instance, $this->id_base);
        $c = $instance['count'] ? '1' : '0';
        $h = $instance['hierarchical'] ? '1' : '0';
        $s = isset($instance['show_children_only']) && $instance['show_children_only'] ? '1' : '0';
        $d = $instance['dropdown'] ? '1' : '0';
        $o = isset($instance['orderby']) ? $instance['orderby'] : 'order';
        echo $before_widget;
        if ($title) {
            echo $before_title . $title . $after_title;
        }
        $cat_args = array('show_count' => $c, 'hierarchical' => $h, 'taxonomy' => 'product_cat');
        if ($o == 'order') {
            $cat_args['menu_order'] = 'asc';
        } else {
            $cat_args['orderby'] = $o;
        }
        if ($d) {
            // Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
            woocommerce_product_dropdown_categories($c, $h);
            ?>
			<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 
        } elseif ($s) {
            global $wp_query, $post;
            $cat_args['title_li'] = '';
            $cat_args['hierarchical'] = 1;
            $cat_args['child_of'] = 0;
            $cat_args['pad_counts'] = 1;
            $cats = get_terms('product_cat', apply_filters('woocommerce_product_categories_widget_args', $cat_args));
            $this->current_cat = false;
            $this->cat_ancestors = array();
            if (is_tax('product_cat')) {
                $this->current_cat = $wp_query->queried_object;
                $this->cat_ancestors = get_ancestors($this->current_cat->term_id, 'product_cat');
            } elseif (is_singular('product')) {
                $product_category = wp_get_post_terms($post->ID, 'product_cat');
                if ($product_category) {
                    $this->current_cat = end($product_category);
                    $this->cat_ancestors = get_ancestors($this->current_cat->term_id, 'product_cat');
                }
            }
            echo '<ul>';
            foreach ($cats as $cat) {
                // Only show top level for now
                if ($cat->parent) {
                    continue;
                }
                echo '<li class="cat-item cat-item-' . $cat->term_id;
                if ($this->current_cat->term_id == $cat->term_id || is_tax('product_cat', $cat->slug)) {
                    echo ' current-cat';
                }
                if ($this->current_cat && in_array($cat->term_id, $this->cat_ancestors)) {
                    echo ' current-cat-parent';
                }
                echo '"><a href="' . get_term_link($cat->slug, 'product_cat') . '">' . $cat->name . '</a>';
                if ($c) {
                    echo ' <span class="count">(' . $cat->count . ')</span>';
                }
                if (is_tax('product_cat', $cat->slug) || in_array($cat->term_id, $this->cat_ancestors)) {
                    $children = $this->get_children_cats($cat->term_id);
                    $this->output_children_cats($children, $c);
                }
                echo '</li>';
            }
            echo '</ul>';
        } else {
            echo '<ul>';
            $cat_args['title_li'] = '';
            wp_list_categories(apply_filters('woocommerce_product_categories_widget_args', $cat_args));
            echo '</ul>';
        }
        echo $after_widget;
    }
function woo_product_categories_dropdown($atts)
{
    extract(shortcode_atts(array('count' => '1', 'hierarchical' => '1', 'orderby' => ''), $atts));
    ob_start();
    $c = $count;
    $h = $hierarchical;
    $o = isset($orderby) && $orderby != '' ? $orderby : 'order';
    // Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
    woocommerce_product_dropdown_categories($c, $h, 0, $o);
    ?>
    <script type='text/javascript'>
        /* <![CDATA[ */
        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;
            }
        }
        product_cat_dropdown.onchange = onProductCatChange;
        /* ]]> */
    </script>
    <?php 
    return ob_get_clean();
}