예제 #1
0
 /**
  * Get chosen filters.
  * 
  * @return array
  */
 public function getChosenFilters()
 {
     // parse url
     $url = $_SERVER['QUERY_STRING'];
     parse_str($url, $query);
     $chosen = array();
     $term_ancestors = array();
     $active_filters = array();
     // keyword
     if (isset($_GET['keyword'])) {
         $keyword = !empty($_GET['keyword']) ? $_GET['keyword'] : '';
         $active_filters['keyword'] = $keyword;
     }
     // orderby
     if (isset($_GET['orderby'])) {
         $orderby = !empty($_GET['orderby']) ? $_GET['orderby'] : '';
         $active_filters['orderby'] = $orderby;
     }
     foreach ($query as $key => $value) {
         // attribute
         if (preg_match('/^attr/', $key) && !empty($query[$key])) {
             $terms = explode(',', $value);
             $new_key = str_replace(array('attra-', 'attro-'), '', $key);
             $taxonomy = 'pa_' . $new_key;
             if (preg_match('/^attra/', $key)) {
                 $query_type = 'and';
             } else {
                 $query_type = 'or';
             }
             $chosen[$taxonomy] = array('terms' => $terms, 'query_type' => $query_type);
             foreach ($terms as $term_id) {
                 $ancestors = wcapf_get_term_ancestors($term_id, $taxonomy);
                 $term_data = wcapf_get_term_data($term_id, $taxonomy);
                 $term_ancestors[$key][] = $ancestors;
                 $active_filters['term'][$key][$term_id] = $term_data->name;
             }
         }
         // category
         if (preg_match('/product-cat/', $key) && !empty($query[$key])) {
             $terms = explode(',', $value);
             $taxonomy = 'product_cat';
             if (preg_match('/^product-cata/', $key)) {
                 $query_type = 'and';
             } else {
                 $query_type = 'or';
             }
             $chosen[$taxonomy] = array('terms' => $terms, 'query_type' => $query_type);
             foreach ($terms as $term_id) {
                 $ancestors = wcapf_get_term_ancestors($term_id, $taxonomy);
                 $term_data = wcapf_get_term_data($term_id, $taxonomy);
                 $term_ancestors[$key][] = $ancestors;
                 $active_filters['term'][$key][$term_id] = $term_data->name;
             }
         }
     }
     // min-price
     if (isset($_GET['min-price'])) {
         $active_filters['min_price'] = $_GET['min-price'];
     }
     // max-price
     if (isset($_GET['max-price'])) {
         $active_filters['max_price'] = $_GET['max-price'];
     }
     return array('chosen' => $chosen, 'term_ancestors' => $term_ancestors, 'active_filters' => $active_filters);
 }
예제 #2
0
 function wcapf_dropdown_sub_terms($sub_term_args, $found, $depth)
 {
     global $wcapf;
     $filtered_product_ids = $wcapf->filteredProductIds();
     $unfiltered_product_ids = $wcapf->unfilteredProductIds();
     $chosen_filters = $wcapf->getChosenFilters();
     extract($sub_term_args);
     $html = '';
     foreach ($sub_term_ids as $sub_term_id) {
         $sub_term_data = wcapf_get_term_data($sub_term_id, $taxonomy);
         if ($sub_term_data && $sub_term_data->parent == $parent_term_id) {
             $_parent_term_id = $sub_term_data->term_id;
             $_parent_term_name = $sub_term_data->name;
             // get sub term ids for this term
             $_sub_term_ids = wcapf_get_term_childs($_parent_term_id, $taxonomy);
             // get product ids for this term
             $products_in_term = wcapf_get_term_products($_parent_term_id, $taxonomy);
             if ($query_type === 'and') {
                 // count product ids those are not present in $filtered_product_ids array
                 $count = sizeof(array_intersect($products_in_term, $filtered_product_ids));
             } else {
                 // count product ids those are present in $unfiltered_product_ids
                 $count = sizeof(array_intersect($products_in_term, $unfiltered_product_ids));
             }
             $force_show = false;
             if (sizeof($ancestors) > 0) {
                 if (in_array($_parent_term_id, $ancestors)) {
                     $force_show = true;
                 }
             }
             if ($count > 0 || $force_show === true) {
                 $found = true;
                 $selected = in_array($_parent_term_id, $term_ids) ? ' selected="selected"' : '';
                 if ($show_count === true) {
                     $html .= '<option value="' . $_parent_term_id . '"' . $selected . ' data-depth="' . $depth . '">' . $_parent_term_name . ' (' . $count . ')</option>';
                 } else {
                     $html .= '<option value="' . $_parent_term_id . '"' . $selected . ' data-depth="' . $depth . '">' . $_parent_term_name . '</option>';
                 }
                 if ($enable_hierarchy === true && $show_children_only !== true || $show_children_only == true && (in_array($_parent_term_id, $term_ids) || $force_show === true)) {
                     if (sizeof($_sub_term_ids) > 0) {
                         $sub_term_args = array('taxonomy' => $taxonomy, 'data_key' => $data_key, 'query_type' => $query_type, 'enable_multiple' => $enable_multiple, 'show_count' => $show_count, 'enable_hierarchy' => $enable_hierarchy, 'show_children_only' => $show_children_only, 'parent_term_id' => $_parent_term_id, 'sub_term_ids' => $_sub_term_ids, 'term_ids' => $term_ids, 'ancestors' => $ancestors);
                         $results = wcapf_dropdown_sub_terms($sub_term_args, $found, $depth + 1);
                         $html .= $results['html'];
                         $found = $results['found'];
                     }
                 }
             }
         }
     }
     return array('html' => $html, 'found' => $found);
 }