__( "Shows a custom attribute in a widget which lets you narrow down the list of shown products in categories.", 'jigoshop'), ); // Create the widget parent::__construct('layered_nav', __('Jigoshop: Layered Nav', 'jigoshop'), $options); } /** * Widget * * Display the widget in the sidebar * * @param array sidebar arguments * @param array instance */ public function widget( $args, $instance ) { // TODO: Optimize this code // Extract the widget arguments extract($args); global $_chosen_attributes, $wpdb, $jigoshop_all_post_ids_in_view; // Hide widget if not product related if ( ! is_product_list() ) return false; // Set the widget title $title = apply_filters( 'widget_title', ( $instance['title'] ) ? $instance['title'] : __( 'Filter by Attributes', 'jigoshop' ), $instance, $this->id_base ); // Check if taxonomy exists $taxonomy = 'pa_'.sanitize_title($instance['attribute']); if ( ! taxonomy_exists($taxonomy) ) return false; // Get all the terms that aren't empty $args = array( 'hide_empty' => true, ); $terms = get_terms( $taxonomy, $args ); $has_terms = (bool) $terms; // If has terms print layered navigation if($has_terms) { $found = false; ob_start(); // Print the widget wrapper & title echo $before_widget; echo $before_title . $title . $after_title; //Remove param link $remove_link = remove_query_arg('filter_'.sanitize_title($instance['attribute'])); echo "Clear"; // Open the list echo ""; // Close the list // Print closing widget wrapper echo $after_widget; if ( ! $found ) { ob_clean(); // clear the buffer return false; // display nothing } else { echo ob_get_clean(); // output the buffer } } } /** * Update * * Handles the processing of information entered in the wordpress admin * * @param array new instance * @param array old instance * @return array instance */ public function update( $new_instance, $old_instance ) { $instance = $old_instance; // Save the new values $instance['title'] = strip_tags(stripslashes($new_instance['title'])); $instance['attribute'] = stripslashes($new_instance['attribute']); return $instance; } /** * Form * * Displays the form for the wordpress admin * * @param array instance */ public function form( $instance ) { global $wpdb; // Get values from instance $title = (isset($instance['title'])) ? esc_attr($instance['title']) : null; $attr_tax = jigoshop_product::getAttributeTaxonomies(); // Widget title echo '

'; echo ''; echo ''; echo '

'; // Print attribute selector if ( ! empty($attr_tax) ) { echo '

'; echo ' '; echo ''; echo '

'; } } } // class Jigoshop_Widget_Layered_Nav function jigoshop_layered_nav_query( $filtered_posts ) { global $_chosen_attributes; if (sizeof($_chosen_attributes)>0) : $matched_products = array(); $filtered = false; foreach ($_chosen_attributes as $attribute => $values) : if (sizeof($values)>0) : foreach ($values as $value) : $posts = get_objects_in_term( $value, $attribute ); if (!is_wp_error($posts) && (sizeof($matched_products)>0 || $filtered)) : $matched_products = array_intersect($posts, $matched_products); elseif (!is_wp_error($posts)) : $matched_products = $posts; endif; $filtered = true; endforeach; endif; endforeach; if ($filtered) : $matched_products[] = 0; $filtered_posts = array_intersect($filtered_posts, $matched_products); endif; endif; return $filtered_posts; } add_filter( 'loop-shop-posts-in', 'jigoshop_layered_nav_query' ); function jigoshop_layered_nav_init() { global $_chosen_attributes; $_chosen_attributes = array(); $attribute_taxonomies = jigoshop_product::getAttributeTaxonomies(); if ( $attribute_taxonomies ) : foreach ($attribute_taxonomies as $tax) : $attribute = sanitize_title($tax->attribute_name); $taxonomy = 'pa_' . $attribute; $name = 'filter_' . $attribute; if (isset($_GET[$name]) && taxonomy_exists($taxonomy)) $_chosen_attributes[$taxonomy] = explode(',', $_GET[$name] ); endforeach; endif; } add_action( 'init', 'jigoshop_layered_nav_init', 1 );