function widget($args, $instance) { global $_chosen_attributes, $woocommerce; extract($args); $_attributes_array = yit_wcan_get_product_taxonomy(); if (apply_filters('yith_wcan_is_search', is_search())) { return; } if (apply_filters('yith_wcan_show_widget', !is_post_type_archive('product') && !is_tax($_attributes_array))) { return; } if (defined('YITH_WCBR_PREMIUM_INIT') && YITH_WCBR_PREMIUM_INIT) { $this->brand_taxonomy = YITH_WCBR::$brands_taxonomy; } $current_term = $_attributes_array && is_tax($_attributes_array) ? get_queried_object()->term_id : ''; $current_tax = $_attributes_array && is_tax($_attributes_array) ? get_queried_object()->taxonomy : ''; $title = apply_filters('yith_widget_title_ajax_navigation', isset($instance['title']) ? $instance['title'] : '', $instance, $this->id_base); $query_type = isset($instance['query_type']) ? $instance['query_type'] : 'and'; $display_type = isset($instance['type']) ? $instance['type'] : 'list'; $is_child_class = 'yit-wcan-child-terms'; $is_chosen_class = 'chosen'; $terms_type_list = isset($instance['display']) && ($display_type == 'list' || $display_type == 'select') ? $instance['display'] : 'all'; $instance['attribute'] = empty($instance['attribute']) ? '' : $instance['attribute']; /* FIX TO WOOCOMMERCE 2.1 */ if (function_exists('wc_attribute_taxonomy_name')) { $taxonomy = wc_attribute_taxonomy_name($instance['attribute']); } else { $taxonomy = $woocommerce->attribute_taxonomy_name($instance['attribute']); } if (!taxonomy_exists($taxonomy)) { return; } $taxonomy = apply_filters('yith_wcan_get_terms_params', $taxonomy, $instance, 'taxonomy_name'); $terms_type_list = apply_filters('yith_wcan_get_terms_params', $terms_type_list, $instance, 'terms_type'); $terms = yit_get_terms($terms_type_list, $taxonomy, $instance); if (count($terms) > 0) { ob_start(); $this->found = false; echo $before_widget; $title = html_entity_decode(apply_filters('widget_title', $title)); if (!empty($title)) { echo $before_title . $title . $after_title; } // Force found when option is selected - do not force found on taxonomy attributes if (!$_attributes_array || !is_tax($_attributes_array)) { if (is_array($_chosen_attributes) && array_key_exists($taxonomy, $_chosen_attributes)) { $this->found = true; } } if (in_array($display_type, apply_filters('yith_wcan_display_type_list', array('list')))) { // List display echo "<ul class='yith-wcan-list yith-wcan'>"; foreach ($terms as $term) { // Get count based on current view - uses transients $transient_name = 'wc_ln_count_' . md5(sanitize_key($taxonomy) . sanitize_key($term->term_id)); //if ( false === ( $_products_in_term = get_transient( $transient_name ) ) ) { $_products_in_term = get_objects_in_term($term->term_id, $taxonomy); set_transient($transient_name, $_products_in_term); //} $option_is_set = isset($_chosen_attributes[$taxonomy]) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']); $term_param = apply_filters('yith_wcan_term_param_uri', $term->term_id, $display_type, $term); // If this is an AND query, only show options with count > 0 if ($query_type == 'and') { $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->filtered_product_ids)); // skip the term for the current archive if ($current_term == $term_param) { continue; } if ($count > 0 && $current_term !== $term_param) { $this->found = true; } if (($terms_type_list == 'hierarchical' || $terms_type_list == 'tags' && $instance['display'] == 'hierarchical' || !yit_term_has_child($term, $taxonomy)) && $count == 0 && !$option_is_set) { continue; } // If this is an OR query, show all options so search can be expanded } else { // skip the term for the current archive if ($current_term == $term_param) { continue; } $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->unfiltered_product_ids)); if ($count > 0) { $this->found = true; } } $arg = apply_filters('yith_wcan_list_type_query_arg', 'filter_' . sanitize_title($instance['attribute']), $display_type, $term); $current_filter = isset($_GET[$arg]) ? explode(apply_filters('yith_wcan_list_filter_operator', ',', $display_type), apply_filters("yith_wcan_list_filter_query_{$arg}", $_GET[$arg])) : array(); if (!is_array($current_filter)) { $current_filter = array(); } $current_filter = array_map('esc_attr', $current_filter); if (!in_array($term_param, $current_filter)) { $current_filter[] = $term_param; } $link = yit_get_woocommerce_layered_nav_link(); // All current filters if ($_chosen_attributes) { foreach ($_chosen_attributes as $name => $data) { if ($name !== $taxonomy) { // Exclude query arg for current term archive term while (in_array($current_term, $data['terms'])) { $key = array_search($current_term, $data); unset($data['terms'][$key]); } // Remove pa_ and sanitize $filter_name = sanitize_title(str_replace('pa_', '', $name)); if (!empty($data['terms'])) { $link = add_query_arg('filter_' . $filter_name, implode(',', $data['terms']), $link); } if ($data['query_type'] == 'or') { $link = add_query_arg('query_type_' . $filter_name, 'or', $link); } } } } // Min/Max if (isset($_GET['min_price'])) { $link = add_query_arg('min_price', $_GET['min_price'], $link); } if (isset($_GET['max_price'])) { $link = add_query_arg('max_price', $_GET['max_price'], $link); } if (isset($_GET['product_tag']) && $display_type != 'tags') { $link = add_query_arg('product_tag', urlencode($_GET['product_tag']), $link); } if (isset($_GET[$this->brand_taxonomy])) { $brands = get_term_by('slug', $_GET[$this->brand_taxonomy], $this->brand_taxonomy); if ($brands->term_id != $term->term_id) { $link = add_query_arg($this->brand_taxonomy, urlencode($brands->slug), $link); } } if (isset($_GET['product_cat'])) { $categories_filter_operator = 'and' == $query_type ? '+' : ','; $_chosen_categories = explode($categories_filter_operator, urlencode($_GET['product_cat'])); $link = add_query_arg('product_cat', implode(apply_filters('yith_wcan_categories_filter_operator', $categories_filter_operator, $display_type), $_chosen_categories), $link); } if (isset($_GET['product_cat'])) { $categories_filter_operator = 'and' == $query_type ? '+' : ','; $_chosen_categories = explode($categories_filter_operator, urlencode($_GET['product_cat'])); $link = add_query_arg('product_cat', implode(apply_filters('yith_wcan_categories_filter_operator', $categories_filter_operator, $display_type), $_chosen_categories), $link); } $check_for_current_widget = isset($_chosen_attributes[$taxonomy]) && is_array($_chosen_attributes[$taxonomy]['terms']) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']); // Current Filter = this widget if (apply_filters('yith_wcan_list_type_current_widget_check', $check_for_current_widget, $current_filter, $display_type, $term_param)) { $class = ($terms_type_list == 'hierarchical' || $terms_type_list == 'tags' && $instance['display'] == 'hierarchical') && yit_term_is_child($term) ? "class='{$is_chosen_class} {$is_child_class}'" : "class='{$is_chosen_class}'"; // Remove this term is $current_filter has more than 1 term filtered if (sizeof($current_filter) > 1) { $current_filter_without_this = array_diff($current_filter, array($term_param)); $link = add_query_arg($arg, implode(apply_filters('yith_wcan_list_filter_operator', ',', $display_type), $current_filter_without_this), $link); } } else { $class = ($terms_type_list == 'hierarchical' || $terms_type_list == 'tags' && $instance['display'] == 'hierarchical') && yit_term_is_child($term) ? "class='{$is_child_class}'" : ''; $link = add_query_arg($arg, implode(apply_filters('yith_wcan_list_filter_operator', ',', $display_type), $current_filter), $link); } // Search Arg if (get_search_query()) { $link = add_query_arg('s', get_search_query(), $link); } // Post Type Arg if (isset($_GET['post_type'])) { $link = add_query_arg('post_type', $_GET['post_type'], $link); } $is_attribute = apply_filters('yith_wcan_is_attribute_check', true); // Query type Arg if ($is_attribute && $query_type == 'or' && !(sizeof($current_filter) == 1 && isset($_chosen_attributes[$taxonomy]['terms']) && is_array($_chosen_attributes[$taxonomy]['terms']) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']))) { $link = add_query_arg('query_type_' . sanitize_title($instance['attribute']), 'or', $link); } echo '<li ' . $class . '>'; echo $count > 0 || $option_is_set ? '<a href="' . esc_url(apply_filters('woocommerce_layered_nav_link', $link)) . '">' : '<span>'; echo $term->name; echo $count > 0 || $option_is_set ? '</a>' : '</span>'; if ($count != 0 && apply_filters("{$args['widget_id']}-show_product_count", true, $instance)) { echo ' <small class="count">' . $count . '</small><div class="clear"></div></li>'; } } echo "</ul>"; } elseif ($display_type == 'select') { $dropdown_label = __('Filters:', 'yith-woocommerce-ajax-navigation'); ?> <a class="yit-wcan-select-open" href="#"><?php echo apply_filters('yith_wcan_dropdown_default_label', $dropdown_label); ?> </a> <?php // Select display echo "<div class='yith-wcan-select-wrapper'>"; echo "<ul class='yith-wcan-select yith-wcan'>"; foreach ($terms as $term) { // Get count based on current view - uses transients $transient_name = 'wc_ln_count_' . md5(sanitize_key($taxonomy) . sanitize_key($term->term_id)); //if ( false === ( $_products_in_term = get_transient( $transient_name ) ) ) { $_products_in_term = get_objects_in_term($term->term_id, $taxonomy); set_transient($transient_name, $_products_in_term); //} $option_is_set = isset($_chosen_attributes[$taxonomy]) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']); // If this is an AND query, only show options with count > 0 if ($query_type == 'and') { $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->filtered_product_ids)); // skip the term for the current archive if ($current_term == $term->term_id) { continue; } if ($count > 0 && $current_term !== $term->term_id) { $this->found = true; } if (($terms_type_list != 'hierarchical' || !yit_term_has_child($term, $taxonomy)) && $count == 0 && !$option_is_set) { continue; } // If this is an OR query, show all options so search can be expanded } else { // skip the term for the current archive if ($current_term == $term->term_id) { continue; } $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->unfiltered_product_ids)); if ($count > 0) { $this->found = true; } } $arg = 'filter_' . urldecode(sanitize_title($instance['attribute'])); $current_filter = isset($_GET[$arg]) ? explode(',', $_GET[$arg]) : array(); if (!is_array($current_filter)) { $current_filter = array(); } $current_filter = array_map('esc_attr', $current_filter); if (!in_array($term->term_id, $current_filter)) { $current_filter[] = $term->term_id; } $link = yit_get_woocommerce_layered_nav_link(); // All current filters if ($_chosen_attributes) { foreach ($_chosen_attributes as $name => $data) { if ($name !== $taxonomy) { // Exclude query arg for current term archive term while (in_array($current_term, $data['terms'])) { $key = array_search($current_term, $data); unset($data['terms'][$key]); } // Remove pa_ and sanitize $filter_name = urldecode(sanitize_title(str_replace('pa_', '', $name))); if (!empty($data['terms'])) { $link = add_query_arg('filter_' . $filter_name, implode(',', $data['terms']), $link); } if ($data['query_type'] == 'or') { $link = add_query_arg('query_type_' . $filter_name, 'or', $link); } } } } // Min/Max if (isset($_GET['min_price'])) { $link = add_query_arg('min_price', $_GET['min_price'], $link); } if (isset($_GET['max_price'])) { $link = add_query_arg('max_price', $_GET['max_price'], $link); } if (isset($_GET['product_tag'])) { $link = add_query_arg('product_tag', urlencode($_GET['product_tag']), $link); } if (isset($_GET[$this->brand_taxonomy])) { $brands = get_term_by('name', $_GET[$this->brand_taxonomy], $this->brand_taxonomy); if ($brands->term_id != $term->term_id) { $link = add_query_arg($this->brand_taxonomy, urlencode($brands->slug), $link); } } if (isset($_GET['product_cat'])) { $categories_filter_operator = 'and' == $query_type ? '+' : ','; $_chosen_categories = explode($categories_filter_operator, urlencode($_GET['product_cat'])); $link = add_query_arg('product_cat', implode(apply_filters('yith_wcan_categories_filter_operator', $categories_filter_operator, $display_type), $_chosen_categories), $link); } // Current Filter = this widget if (isset($_chosen_attributes[$taxonomy]) && is_array($_chosen_attributes[$taxonomy]['terms']) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms'])) { $class = $terms_type_list == 'hierarchical' && yit_term_is_child($term) ? "class='{$is_chosen_class} {$is_child_class}'" : "class='{$is_chosen_class}'"; // Remove this term is $current_filter has more than 1 term filtered if (sizeof($current_filter) > 1) { $current_filter_without_this = array_diff($current_filter, array($term->term_id)); $link = add_query_arg($arg, implode(',', $current_filter_without_this), $link); } } else { $class = $terms_type_list == 'hierarchical' && yit_term_is_child($term) ? "class='{$is_child_class}'" : ''; $link = add_query_arg($arg, implode(',', $current_filter), $link); } // Search Arg if (get_search_query()) { $link = add_query_arg('s', get_search_query(), $link); } // Post Type Arg if (isset($_GET['post_type'])) { $link = add_query_arg('post_type', $_GET['post_type'], $link); } // Query type Arg if ($query_type == 'or' && !(sizeof($current_filter) == 1 && isset($_chosen_attributes[$taxonomy]['terms']) && is_array($_chosen_attributes[$taxonomy]['terms']) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']))) { $link = add_query_arg('query_type_' . sanitize_title($instance['attribute']), 'or', $link); } echo '<li ' . $class . '>'; echo $count > 0 || $option_is_set ? '<a data-type="select" href="' . esc_url(apply_filters('woocommerce_layered_nav_link', $link)) . '">' : '<span>'; echo $term->name; echo $count > 0 || $option_is_set ? '</a>' : '</span>'; echo '</li>'; } echo "</ul>"; echo "</div>"; } elseif ($display_type == 'color') { // List display echo "<ul class='yith-wcan-color yith-wcan yith-wcan-group'>"; foreach ($terms as $term) { // Get count based on current view - uses transients $transient_name = 'wc_ln_count_' . md5(sanitize_key($taxonomy) . sanitize_key($term->term_id)); //if ( false === ( $_products_in_term = get_transient( $transient_name ) ) ) { $_products_in_term = get_objects_in_term($term->term_id, $taxonomy); set_transient($transient_name, $_products_in_term); //} $option_is_set = isset($_chosen_attributes[$taxonomy]) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']); // If this is an AND query, only show options with count > 0 if ($query_type == 'and') { $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->filtered_product_ids)); // skip the term for the current archive if ($current_term == $term->term_id) { continue; } if ($count > 0 && $current_term !== $term->term_id) { $this->found = true; } if ($count == 0 && !$option_is_set) { continue; } // If this is an OR query, show all options so search can be expanded } else { // skip the term for the current archive if ($current_term == $term->term_id) { continue; } $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->unfiltered_product_ids)); if ($count > 0) { $this->found = true; } } $arg = 'filter_' . sanitize_title($instance['attribute']); $current_filter = isset($_GET[$arg]) ? explode(',', $_GET[$arg]) : array(); if (!is_array($current_filter)) { $current_filter = array(); } $current_filter = array_map('esc_attr', $current_filter); if (!in_array($term->term_id, $current_filter)) { $current_filter[] = $term->term_id; } $link = yit_get_woocommerce_layered_nav_link(); // All current filters if ($_chosen_attributes) { foreach ($_chosen_attributes as $name => $data) { if ($name !== $taxonomy) { // Exclude query arg for current term archive term while (in_array($current_term, $data['terms'])) { $key = array_search($current_term, $data); unset($data['terms'][$key]); } // Remove pa_ and sanitize $filter_name = sanitize_title(str_replace('pa_', '', $name)); if (!empty($data['terms'])) { $link = add_query_arg('filter_' . $filter_name, implode(',', $data['terms']), $link); } if ($data['query_type'] == 'or') { $link = add_query_arg('query_type_' . $filter_name, 'or', $link); } } } } // Min/Max if (isset($_GET['min_price'])) { $link = add_query_arg('min_price', $_GET['min_price'], $link); } if (isset($_GET['max_price'])) { $link = add_query_arg('max_price', $_GET['max_price'], $link); } if (isset($_GET['product_tag'])) { $link = add_query_arg('product_tag', urlencode($_GET['product_tag']), $link); } if (isset($_GET[$this->brand_taxonomy])) { $brands = get_term_by('name', $_GET[$this->brand_taxonomy], $this->brand_taxonomy); if ($brands->term_id != $term->term_id) { $link = add_query_arg($this->brand_taxonomy, urlencode($brands->slug), $link); } } if (isset($_GET['product_cat'])) { $categories_filter_operator = 'and' == $query_type ? '+' : ','; $_chosen_categories = explode($categories_filter_operator, urlencode($_GET['product_cat'])); $link = add_query_arg('product_cat', implode(apply_filters('yith_wcan_categories_filter_operator', $categories_filter_operator, $display_type), $_chosen_categories), $link); } // Current Filter = this widget if (isset($_chosen_attributes[$taxonomy]) && is_array($_chosen_attributes[$taxonomy]['terms']) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms'])) { $class = $terms_type_list == 'hierarchical' && yit_term_is_child($term) ? "class='{$is_chosen_class} {$is_child_class}'" : "class='{$is_chosen_class}'"; // Remove this term is $current_filter has more than 1 term filtered if (sizeof($current_filter) > 1) { $current_filter_without_this = array_diff($current_filter, array($term->term_id)); $link = add_query_arg($arg, implode(',', $current_filter_without_this), $link); } } else { $class = $terms_type_list == 'hierarchical' && yit_term_is_child($term) ? "class='{$is_child_class}'" : ''; $link = add_query_arg($arg, implode(',', $current_filter), $link); } // Search Arg if (get_search_query()) { $link = add_query_arg('s', get_search_query(), $link); } // Post Type Arg if (isset($_GET['post_type'])) { $link = add_query_arg('post_type', $_GET['post_type'], $link); } // Query type Arg if ($query_type == 'or' && !(sizeof($current_filter) == 1 && isset($_chosen_attributes[$taxonomy]['terms']) && is_array($_chosen_attributes[$taxonomy]['terms']) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']))) { $link = add_query_arg('query_type_' . sanitize_title($instance['attribute']), 'or', $link); } $term_id = yit_wcan_localize_terms($term->term_id, $taxonomy); if (!empty($instance['colors'][$term_id])) { $li_style = apply_filters("{$args['widget_id']}-li_style", 'background-color:' . $instance['colors'][$term_id] . ';', $instance); echo '<li ' . $class . '>'; echo $count > 0 || $option_is_set ? '<a style="' . $li_style . '" href="' . esc_url(apply_filters('woocommerce_layered_nav_link', $link)) . '" title="' . $term->name . '" >' : '<span style="background-color:' . $instance['colors'][$term_id] . ';" >'; echo $term->name; echo $count > 0 || $option_is_set ? '</a>' : '</span>'; } } echo "</ul>"; } elseif ($display_type == 'label') { // List display echo "<ul class='yith-wcan-label yith-wcan yith-wcan-group'>"; foreach ($terms as $term) { // Get count based on current view - uses transients $transient_name = 'wc_ln_count_' . md5(sanitize_key($taxonomy) . sanitize_key($term->term_id)); //if ( false === ( $_products_in_term = get_transient( $transient_name ) ) ) { $_products_in_term = get_objects_in_term($term->term_id, $taxonomy); set_transient($transient_name, $_products_in_term); //} $option_is_set = isset($_chosen_attributes[$taxonomy]) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']); // If this is an AND query, only show options with count > 0 if ($query_type == 'and') { $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->filtered_product_ids)); // skip the term for the current archive if ($current_term == $term->term_id) { continue; } if ($count > 0 && $current_term !== $term->term_id) { $this->found = true; } if ($count == 0 && !$option_is_set) { continue; } // If this is an OR query, show all options so search can be expanded } else { // skip the term for the current archive if ($current_term == $term->term_id) { continue; } $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->unfiltered_product_ids)); if ($count > 0) { $this->found = true; } } $arg = 'filter_' . sanitize_title($instance['attribute']); $current_filter = isset($_GET[$arg]) ? explode(',', $_GET[$arg]) : array(); if (!is_array($current_filter)) { $current_filter = array(); } $current_filter = array_map('esc_attr', $current_filter); if (!in_array($term->term_id, $current_filter)) { $current_filter[] = $term->term_id; } $link = yit_get_woocommerce_layered_nav_link(); // All current filters if ($_chosen_attributes) { foreach ($_chosen_attributes as $name => $data) { if ($name !== $taxonomy) { // Exclude query arg for current term archive term while (in_array($current_term, $data['terms'])) { $key = array_search($current_term, $data); unset($data['terms'][$key]); } // Remove pa_ and sanitize $filter_name = sanitize_title(str_replace('pa_', '', $name)); if (!empty($data['terms'])) { $link = add_query_arg('filter_' . $filter_name, implode(',', $data['terms']), $link); } if ($data['query_type'] == 'or') { $link = add_query_arg('query_type_' . $filter_name, 'or', $link); } } } } // Min/Max if (isset($_GET['min_price'])) { $link = add_query_arg('min_price', $_GET['min_price'], $link); } if (isset($_GET['max_price'])) { $link = add_query_arg('max_price', $_GET['max_price'], $link); } if (isset($_GET['product_tag'])) { $link = add_query_arg('product_tag', urlencode($_GET['product_tag']), $link); } if (isset($_GET[$this->brand_taxonomy])) { $brands = get_term_by('name', $_GET[$this->brand_taxonomy], $this->brand_taxonomy); if ($brands->term_id != $term->term_id) { $link = add_query_arg($this->brand_taxonomy, urlencode($brands->slug), $link); } } if (isset($_GET['product_cat'])) { $categories_filter_operator = 'and' == $query_type ? '+' : ','; $_chosen_categories = explode($categories_filter_operator, urlencode($_GET['product_cat'])); $link = add_query_arg('product_cat', implode(apply_filters('yith_wcan_categories_filter_operator', $categories_filter_operator, $display_type), $_chosen_categories), $link); } // Current Filter = this widget if (isset($_chosen_attributes[$taxonomy]) && is_array($_chosen_attributes[$taxonomy]['terms']) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms'])) { $class = $terms_type_list == 'hierarchical' && yit_term_is_child($term) ? "class='{$is_chosen_class} {$is_child_class}'" : "class='{$is_chosen_class}'"; // Remove this term is $current_filter has more than 1 term filtered if (sizeof($current_filter) > 1) { $current_filter_without_this = array_diff($current_filter, array($term->term_id)); $link = add_query_arg($arg, implode(',', $current_filter_without_this), $link); } } else { $class = $terms_type_list == 'hierarchical' && yit_term_is_child($term) ? "class='{$is_child_class}'" : ''; $link = add_query_arg($arg, implode(',', $current_filter), $link); } // Search Arg if (get_search_query()) { $link = add_query_arg('s', get_search_query(), $link); } // Post Type Arg if (isset($_GET['post_type'])) { $link = add_query_arg('post_type', $_GET['post_type'], $link); } // Query type Arg if ($query_type == 'or' && !(sizeof($current_filter) == 1 && isset($_chosen_attributes[$taxonomy]['terms']) && is_array($_chosen_attributes[$taxonomy]['terms']) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']))) { $link = add_query_arg('query_type_' . sanitize_title($instance['attribute']), 'or', $link); } $term_id = yit_wcan_localize_terms($term->term_id, $taxonomy); if (!empty($instance['labels'][$term_id])) { echo '<li ' . $class . '>'; echo $count > 0 || $option_is_set ? '<a title="' . $term->name . '" href="' . esc_url(apply_filters('woocommerce_layered_nav_link', $link)) . '">' : '<span>'; echo $instance['labels'][$term_id]; echo $count > 0 || $option_is_set ? '</a>' : '</span>'; } } echo "</ul>"; } else { do_action("yith_wcan_widget_display_{$display_type}", $args, $instance, $display_type, $terms, $taxonomy); } // End display type conditional echo $after_widget; if (!$this->found) { ob_end_clean(); echo substr($before_widget, 0, strlen($before_widget) - 1) . ' style="display:none">' . $after_widget; } else { echo ob_get_clean(); } } }
function widget($args, $instance) { global $_chosen_attributes, $woocommerce, $_attributes_array; extract($args); if (!is_post_type_archive('product') && !is_tax(array_merge($_attributes_array, array('product_cat', 'product_tag')))) { return; } $current_term = $_attributes_array && is_tax($_attributes_array) ? get_queried_object()->term_id : ''; $current_tax = $_attributes_array && is_tax($_attributes_array) ? get_queried_object()->taxonomy : ''; $title = apply_filters('widget_title', isset($instance['title']) ? $instance['title'] : '', $instance, $this->id_base); $query_type = isset($instance['query_type']) ? $instance['query_type'] : 'and'; $display_type = isset($instance['type']) ? $instance['type'] : 'list'; $is_child_class = 'yit-wcan-child-terms'; $is_chosen_class = 'chosen'; $terms_type_list = isset($instance['display']) && ($display_type == 'list' || $display_type == 'select') ? $instance['display'] : 'all'; /* FIX TO WOOCOMMERCE 2.1 */ if (function_exists('wc_attribute_taxonomy_name')) { $taxonomy = wc_attribute_taxonomy_name($instance['attribute']); } else { $taxonomy = $woocommerce->attribute_taxonomy_name($instance['attribute']); } if (!taxonomy_exists($taxonomy)) { return; } $terms = yit_get_terms($terms_type_list, $taxonomy); if (count($terms) > 0) { ob_start(); $found = false; echo $before_widget . $before_title . $title . $after_title; // Force found when option is selected - do not force found on taxonomy attributes if (!$_attributes_array || !is_tax($_attributes_array)) { if (is_array($_chosen_attributes) && array_key_exists($taxonomy, $_chosen_attributes)) { $found = true; } } if ($display_type == 'list') { // List display echo "<ul class='yith-wcan-list yith-wcan'>"; foreach ($terms as $term) { // Get count based on current view - uses transients $transient_name = 'wc_ln_count_' . md5(sanitize_key($taxonomy) . sanitize_key($term->term_id)); //if ( false === ( $_products_in_term = get_transient( $transient_name ) ) ) { $_products_in_term = get_objects_in_term($term->term_id, $taxonomy); set_transient($transient_name, $_products_in_term); //} $option_is_set = isset($_chosen_attributes[$taxonomy]) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']); // If this is an AND query, only show options with count > 0 if ($query_type == 'and') { $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->filtered_product_ids)); // skip the term for the current archive if ($current_term == $term->term_id) { continue; } if ($count > 0 && $current_term !== $term->term_id) { $found = true; } if (($terms_type_list != 'hierarchical' || !yit_term_has_child($term, $taxonomy)) && $count == 0 && !$option_is_set) { continue; } // If this is an OR query, show all options so search can be expanded } else { // skip the term for the current archive if ($current_term == $term->term_id) { continue; } $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->unfiltered_product_ids)); if ($count > 0) { $found = true; } } $arg = 'filter_' . sanitize_title($instance['attribute']); $current_filter = isset($_GET[$arg]) ? explode(',', $_GET[$arg]) : array(); if (!is_array($current_filter)) { $current_filter = array(); } $current_filter = array_map('esc_attr', $current_filter); if (!in_array($term->term_id, $current_filter)) { $current_filter[] = $term->term_id; } // Base Link decided by current page if (defined('SHOP_IS_ON_FRONT')) { $link = home_url(); } elseif (is_post_type_archive('product') || is_page(function_exists('wc_get_page_id') ? wc_get_page_id('shop') : woocommerce_get_page_id('shop'))) { $link = get_post_type_archive_link('product'); } else { $link = get_term_link(get_query_var('term'), get_query_var('taxonomy')); } // All current filters if ($_chosen_attributes) { foreach ($_chosen_attributes as $name => $data) { if ($name !== $taxonomy) { // Exclude query arg for current term archive term while (in_array($current_term, $data['terms'])) { $key = array_search($current_term, $data); unset($data['terms'][$key]); } // Remove pa_ and sanitize $filter_name = sanitize_title(str_replace('pa_', '', $name)); if (!empty($data['terms'])) { $link = add_query_arg('filter_' . $filter_name, implode(',', $data['terms']), $link); } if ($data['query_type'] == 'or') { $link = add_query_arg('query_type_' . $filter_name, 'or', $link); } } } } // Min/Max if (isset($_GET['min_price'])) { $link = add_query_arg('min_price', $_GET['min_price'], $link); } if (isset($_GET['max_price'])) { $link = add_query_arg('max_price', $_GET['max_price'], $link); } // Current Filter = this widget if (isset($_chosen_attributes[$taxonomy]) && is_array($_chosen_attributes[$taxonomy]['terms']) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms'])) { $class = $terms_type_list == 'hierarchical' && yit_term_is_child($term) ? "class='{$is_chosen_class} {$is_child_class}'" : "class='{$is_chosen_class}'"; // Remove this term is $current_filter has more than 1 term filtered if (sizeof($current_filter) > 1) { $current_filter_without_this = array_diff($current_filter, array($term->term_id)); $link = add_query_arg($arg, implode(',', $current_filter_without_this), $link); } } else { $class = $terms_type_list == 'hierarchical' && yit_term_is_child($term) ? "class='{$is_child_class}'" : ''; $link = add_query_arg($arg, implode(',', $current_filter), $link); } // Search Arg if (get_search_query()) { $link = add_query_arg('s', get_search_query(), $link); } // Post Type Arg if (isset($_GET['post_type'])) { $link = add_query_arg('post_type', $_GET['post_type'], $link); } // Query type Arg if ($query_type == 'or' && !(sizeof($current_filter) == 1 && isset($_chosen_attributes[$taxonomy]['terms']) && is_array($_chosen_attributes[$taxonomy]['terms']) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']))) { $link = add_query_arg('query_type_' . sanitize_title($instance['attribute']), 'or', $link); } echo '<li ' . $class . '>'; echo $count > 0 || $option_is_set ? '<a href="' . esc_url(apply_filters('woocommerce_layered_nav_link', $link)) . '">' : '<span>'; echo $term->name; echo $count > 0 || $option_is_set ? '</a>' : '</span>'; if ($count != 0) { echo ' <small class="count">' . $count . '</small><div class="clear"></div></li>'; } } echo "</ul>"; } elseif ($display_type == 'select') { ?> <a class="yit-wcan-select-open" href="#"><?php _e('Filters List:', 'yit'); ?> </a> <?php // Select display echo "<div class='yith-wcan-select-wrapper'>"; echo "<ul class='yith-wcan-select yith-wcan'>"; foreach ($terms as $term) { // Get count based on current view - uses transients $transient_name = 'wc_ln_count_' . md5(sanitize_key($taxonomy) . sanitize_key($term->term_id)); //if ( false === ( $_products_in_term = get_transient( $transient_name ) ) ) { $_products_in_term = get_objects_in_term($term->term_id, $taxonomy); set_transient($transient_name, $_products_in_term); //} $option_is_set = isset($_chosen_attributes[$taxonomy]) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']); // If this is an AND query, only show options with count > 0 if ($query_type == 'and') { $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->filtered_product_ids)); // skip the term for the current archive if ($current_term == $term->term_id) { continue; } if ($count > 0 && $current_term !== $term->term_id) { $found = true; } if (($terms_type_list != 'hierarchical' || !yit_term_has_child($term, $taxonomy)) && $count == 0 && !$option_is_set) { continue; } // If this is an OR query, show all options so search can be expanded } else { // skip the term for the current archive if ($current_term == $term->term_id) { continue; } $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->unfiltered_product_ids)); if ($count > 0) { $found = true; } } $arg = 'filter_' . urldecode(sanitize_title($instance['attribute'])); $current_filter = isset($_GET[$arg]) ? explode(',', $_GET[$arg]) : array(); if (!is_array($current_filter)) { $current_filter = array(); } $current_filter = array_map('esc_attr', $current_filter); if (!in_array($term->term_id, $current_filter)) { $current_filter[] = $term->term_id; } // Base Link decided by current page if (defined('SHOP_IS_ON_FRONT')) { $link = home_url(); } elseif (is_post_type_archive('product') || is_page(woocommerce_get_page_id('shop'))) { $link = get_post_type_archive_link('product'); } else { $link = get_term_link(get_query_var('term'), get_query_var('taxonomy')); } // All current filters if ($_chosen_attributes) { foreach ($_chosen_attributes as $name => $data) { if ($name !== $taxonomy) { // Exclude query arg for current term archive term while (in_array($current_term, $data['terms'])) { $key = array_search($current_term, $data); unset($data['terms'][$key]); } // Remove pa_ and sanitize $filter_name = urldecode(sanitize_title(str_replace('pa_', '', $name))); if (!empty($data['terms'])) { $link = add_query_arg('filter_' . $filter_name, implode(',', $data['terms']), $link); } if ($data['query_type'] == 'or') { $link = add_query_arg('query_type_' . $filter_name, 'or', $link); } } } } // Min/Max if (isset($_GET['min_price'])) { $link = add_query_arg('min_price', $_GET['min_price'], $link); } if (isset($_GET['max_price'])) { $link = add_query_arg('max_price', $_GET['max_price'], $link); } // Current Filter = this widget if (isset($_chosen_attributes[$taxonomy]) && is_array($_chosen_attributes[$taxonomy]['terms']) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms'])) { $class = $terms_type_list == 'hierarchical' && yit_term_is_child($term) ? "class='{$is_chosen_class} {$is_child_class}'" : "class='{$is_chosen_class}'"; // Remove this term is $current_filter has more than 1 term filtered if (sizeof($current_filter) > 1) { $current_filter_without_this = array_diff($current_filter, array($term->term_id)); $link = add_query_arg($arg, implode(',', $current_filter_without_this), $link); } } else { $class = $terms_type_list == 'hierarchical' && yit_term_is_child($term) ? "class='{$is_child_class}'" : ''; $link = add_query_arg($arg, implode(',', $current_filter), $link); } // Search Arg if (get_search_query()) { $link = add_query_arg('s', get_search_query(), $link); } // Post Type Arg if (isset($_GET['post_type'])) { $link = add_query_arg('post_type', $_GET['post_type'], $link); } // Query type Arg if ($query_type == 'or' && !(sizeof($current_filter) == 1 && isset($_chosen_attributes[$taxonomy]['terms']) && is_array($_chosen_attributes[$taxonomy]['terms']) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']))) { $link = add_query_arg('query_type_' . sanitize_title($instance['attribute']), 'or', $link); } echo '<li ' . $class . '>'; echo $count > 0 || $option_is_set ? '<a data-type="select" href="' . esc_url(apply_filters('woocommerce_layered_nav_link', $link)) . '">' : '<span>'; echo $term->name; echo $count > 0 || $option_is_set ? '</a>' : '</span>'; echo '</li>'; } echo "</ul>"; echo "</div>"; } elseif ($display_type == 'color') { // List display echo "<ul class='yith-wcan-color yith-wcan yith-wcan-group'>"; foreach ($terms as $term) { // Get count based on current view - uses transients $transient_name = 'wc_ln_count_' . md5(sanitize_key($taxonomy) . sanitize_key($term->term_id)); //if ( false === ( $_products_in_term = get_transient( $transient_name ) ) ) { $_products_in_term = get_objects_in_term($term->term_id, $taxonomy); set_transient($transient_name, $_products_in_term); //} $option_is_set = isset($_chosen_attributes[$taxonomy]) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']); // If this is an AND query, only show options with count > 0 if ($query_type == 'and') { $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->filtered_product_ids)); // skip the term for the current archive if ($current_term == $term->term_id) { continue; } if ($count > 0 && $current_term !== $term->term_id) { $found = true; } if ($count == 0 && !$option_is_set) { continue; } // If this is an OR query, show all options so search can be expanded } else { // skip the term for the current archive if ($current_term == $term->term_id) { continue; } $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->unfiltered_product_ids)); if ($count > 0) { $found = true; } } $arg = 'filter_' . sanitize_title($instance['attribute']); $current_filter = isset($_GET[$arg]) ? explode(',', $_GET[$arg]) : array(); if (!is_array($current_filter)) { $current_filter = array(); } $current_filter = array_map('esc_attr', $current_filter); if (!in_array($term->term_id, $current_filter)) { $current_filter[] = $term->term_id; } // Base Link decided by current page if (defined('SHOP_IS_ON_FRONT')) { $link = home_url(); } elseif (is_post_type_archive('product') || is_page(function_exists('wc_get_page_id') ? wc_get_page_id('shop') : woocommerce_get_page_id('shop'))) { $link = get_post_type_archive_link('product'); } else { $link = get_term_link(get_query_var('term'), get_query_var('taxonomy')); } // All current filters if ($_chosen_attributes) { foreach ($_chosen_attributes as $name => $data) { if ($name !== $taxonomy) { // Exclude query arg for current term archive term while (in_array($current_term, $data['terms'])) { $key = array_search($current_term, $data); unset($data['terms'][$key]); } // Remove pa_ and sanitize $filter_name = sanitize_title(str_replace('pa_', '', $name)); if (!empty($data['terms'])) { $link = add_query_arg('filter_' . $filter_name, implode(',', $data['terms']), $link); } if ($data['query_type'] == 'or') { $link = add_query_arg('query_type_' . $filter_name, 'or', $link); } } } } // Min/Max if (isset($_GET['min_price'])) { $link = add_query_arg('min_price', $_GET['min_price'], $link); } if (isset($_GET['max_price'])) { $link = add_query_arg('max_price', $_GET['max_price'], $link); } // Current Filter = this widget if (isset($_chosen_attributes[$taxonomy]) && is_array($_chosen_attributes[$taxonomy]['terms']) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms'])) { $class = $terms_type_list == 'hierarchical' && yit_term_is_child($term) ? "class='{$is_chosen_class} {$is_child_class}'" : "class='{$is_chosen_class}'"; // Remove this term is $current_filter has more than 1 term filtered if (sizeof($current_filter) > 1) { $current_filter_without_this = array_diff($current_filter, array($term->term_id)); $link = add_query_arg($arg, implode(',', $current_filter_without_this), $link); } } else { $class = $terms_type_list == 'hierarchical' && yit_term_is_child($term) ? "class='{$is_child_class}'" : ''; $link = add_query_arg($arg, implode(',', $current_filter), $link); } // Search Arg if (get_search_query()) { $link = add_query_arg('s', get_search_query(), $link); } // Post Type Arg if (isset($_GET['post_type'])) { $link = add_query_arg('post_type', $_GET['post_type'], $link); } // Query type Arg if ($query_type == 'or' && !(sizeof($current_filter) == 1 && isset($_chosen_attributes[$taxonomy]['terms']) && is_array($_chosen_attributes[$taxonomy]['terms']) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']))) { $link = add_query_arg('query_type_' . sanitize_title($instance['attribute']), 'or', $link); } if ($instance['colors'][$term->term_id] != '') { echo '<li ' . $class . '>'; echo $count > 0 || $option_is_set ? '<a style="background-color:' . $instance['colors'][$term->term_id] . ';" href="' . esc_url(apply_filters('woocommerce_layered_nav_link', $link)) . '" title="' . $term->name . '" >' : '<span style="background-color:' . $instance['colors'][$term->term_id] . ';" >'; echo $term->name; echo $count > 0 || $option_is_set ? '</a>' : '</span>'; } } echo "</ul>"; } elseif ($display_type == 'label') { // List display echo "<ul class='yith-wcan-label yith-wcan yith-wcan-group'>"; foreach ($terms as $term) { // Get count based on current view - uses transients $transient_name = 'wc_ln_count_' . md5(sanitize_key($taxonomy) . sanitize_key($term->term_id)); //if ( false === ( $_products_in_term = get_transient( $transient_name ) ) ) { $_products_in_term = get_objects_in_term($term->term_id, $taxonomy); set_transient($transient_name, $_products_in_term); //} $option_is_set = isset($_chosen_attributes[$taxonomy]) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']); // If this is an AND query, only show options with count > 0 if ($query_type == 'and') { $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->filtered_product_ids)); // skip the term for the current archive if ($current_term == $term->term_id) { continue; } if ($count > 0 && $current_term !== $term->term_id) { $found = true; } if ($count == 0 && !$option_is_set) { continue; } // If this is an OR query, show all options so search can be expanded } else { // skip the term for the current archive if ($current_term == $term->term_id) { continue; } $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->unfiltered_product_ids)); if ($count > 0) { $found = true; } } $arg = 'filter_' . sanitize_title($instance['attribute']); $current_filter = isset($_GET[$arg]) ? explode(',', $_GET[$arg]) : array(); if (!is_array($current_filter)) { $current_filter = array(); } $current_filter = array_map('esc_attr', $current_filter); if (!in_array($term->term_id, $current_filter)) { $current_filter[] = $term->term_id; } // Base Link decided by current page if (defined('SHOP_IS_ON_FRONT')) { $link = home_url(); } elseif (is_post_type_archive('product') || is_page(function_exists('wc_get_page_id') ? wc_get_page_id('shop') : woocommerce_get_page_id('shop'))) { $link = get_post_type_archive_link('product'); } else { $link = get_term_link(get_query_var('term'), get_query_var('taxonomy')); } // All current filters if ($_chosen_attributes) { foreach ($_chosen_attributes as $name => $data) { if ($name !== $taxonomy) { // Exclude query arg for current term archive term while (in_array($current_term, $data['terms'])) { $key = array_search($current_term, $data); unset($data['terms'][$key]); } // Remove pa_ and sanitize $filter_name = sanitize_title(str_replace('pa_', '', $name)); if (!empty($data['terms'])) { $link = add_query_arg('filter_' . $filter_name, implode(',', $data['terms']), $link); } if ($data['query_type'] == 'or') { $link = add_query_arg('query_type_' . $filter_name, 'or', $link); } } } } // Min/Max if (isset($_GET['min_price'])) { $link = add_query_arg('min_price', $_GET['min_price'], $link); } if (isset($_GET['max_price'])) { $link = add_query_arg('max_price', $_GET['max_price'], $link); } // Current Filter = this widget if (isset($_chosen_attributes[$taxonomy]) && is_array($_chosen_attributes[$taxonomy]['terms']) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms'])) { $class = $terms_type_list == 'hierarchical' && yit_term_is_child($term) ? "class='{$is_chosen_class} {$is_child_class}'" : "class='{$is_chosen_class}'"; // Remove this term is $current_filter has more than 1 term filtered if (sizeof($current_filter) > 1) { $current_filter_without_this = array_diff($current_filter, array($term->term_id)); $link = add_query_arg($arg, implode(',', $current_filter_without_this), $link); } } else { $class = $terms_type_list == 'hierarchical' && yit_term_is_child($term) ? "class='{$is_child_class}'" : ''; $link = add_query_arg($arg, implode(',', $current_filter), $link); } // Search Arg if (get_search_query()) { $link = add_query_arg('s', get_search_query(), $link); } // Post Type Arg if (isset($_GET['post_type'])) { $link = add_query_arg('post_type', $_GET['post_type'], $link); } // Query type Arg if ($query_type == 'or' && !(sizeof($current_filter) == 1 && isset($_chosen_attributes[$taxonomy]['terms']) && is_array($_chosen_attributes[$taxonomy]['terms']) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']))) { $link = add_query_arg('query_type_' . sanitize_title($instance['attribute']), 'or', $link); } if ($instance['labels'][$term->term_id] != '') { echo '<li ' . $class . '>'; echo $count > 0 || $option_is_set ? '<a title="' . $term->name . '" href="' . esc_url(apply_filters('woocommerce_layered_nav_link', $link)) . '">' : '<span>'; echo $instance['labels'][$term->term_id]; echo $count > 0 || $option_is_set ? '</a>' : '</span>'; } } echo "</ul>"; } // End display type conditional echo $after_widget; if (!$found) { ob_end_clean(); echo substr($before_widget, 0, strlen($before_widget) - 1) . ' style="display:none">' . $after_widget; } else { echo ob_get_clean(); } } }