/** * Widget * Display the widget in the sidebar * Save output to the cache if empty * * @param array sidebar arguments * @param array instance */ public function widget($args, $instance) { // Otherwise Start buffering and output the Widget ob_start(); extract($args); // Set the widget title $title = apply_filters('widget_title', $instance['title'] ? $instance['title'] : __('Product Categories', 'fflcommerce'), $instance, $this->id_base); // Get options $count = (bool) isset($instance['count']) ? $instance['count'] : false; $is_hierarchial = (bool) isset($instance['hierarchical']) ? $instance['hierarchical'] : false; $is_dropdown = (bool) isset($instance['dropdown']) ? $instance['dropdown'] : false; // Print the widget wrapper & title echo $before_widget; if ($title) { echo $before_title . $title . $after_title; } // Define options for the list $args = array('orderby' => 'name', 'show_count' => $count, 'hierarchical' => $is_hierarchial, 'taxonomy' => 'product_cat', 'title_li' => null); if (is_product()) { global $post; $categories = get_the_terms($post->ID, 'product_cat'); if (!empty($categories)) { foreach ($categories as $id => $cat) { $args['current_category'] = apply_filters('fflcommerce_product_cat_widget_terms', $cat->term_id, $categories); break; // we can only take the first one } } } // Output as dropdown or unordered list if ($is_dropdown) { // Set up arguments $args['name'] = 'dropdown_product_cat'; // Print dropdown // wp_dropdown_categories($args); Commented out due to wordpress bug 13258 not supporting custom taxonomies // See: http://core.trac.wordpress.org/ticket/13258 fflcommerce_product_dropdown_categories($args['show_count'], $args['hierarchical']); // TODO: Move this javascript to its own file (plugins.js?) ?> <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; } else { location.href = "<?php echo get_permalink(fflcommerce_get_page_id('shop')); ?> " } } dropdown.onchange = onCatChange; /* ]]> */ </script> <?php } else { // Print list of categories echo '<ul>'; wp_list_categories(apply_filters('widget_product_categories_args', $args)); echo '</ul>'; } // Print closing widget wrapper echo $after_widget; // Flush output buffer and save to transient cache $result = ob_get_flush(); $cache[$this->id] = $result; set_transient('fflcommerce_widget_cache', $cache, 3600 * 3); // 3 hours ahead }
function fflcommerce_products_by_category() { global $typenow, $wp_query; if ($typenow == 'product') { fflcommerce_product_dropdown_categories(); } }