|
////// Layered Nav ////// |
|
/** |
|
* fin_woo_layered_nav |
|
* |
|
* adds woocommerce layered_nav widgets for every global attribute to a unique hidden sidebar |
|
**/ |
|
function fin_woo_layered_nav() { |
|
global $woocommerce; |
|
if(is_post_type_archive('product') || is_tax(array('product_cat', 'product_tag'))) { |
|
$attribute_taxonomies = wc_get_attribute_taxonomies(); |
|
foreach($attribute_taxonomies as $attribute) { |
|
$instance = array( |
|
'title' => $attribute->attribute_label, |
|
'attribute' => $attribute->attribute_name, |
|
'query_type' => 'and', |
|
'display_type' => 'list' |
|
); |
|
$args = array(); |
|
//output the widget |
|
the_widget('WC_Widget_Layered_Nav', $instance, $args); |
|
} |
|
} |
|
} |
|
add_filter('fin_after_widgets', 'fin_woo_layered_nav'); |
|
|
|
function fin_woo_layered_nav_init() { |
|
global $_chosen_attributes; |
|
$_chosen_attributes = array(); |
|
$attribute_taxonomies = wc_get_attribute_taxonomies(); |
|
if ($attribute_taxonomies) { |
|
foreach ($attribute_taxonomies as $tax) { |
|
$attribute = wc_sanitize_taxonomy_name( $tax->attribute_name ); |
|
$taxonomy = wc_attribute_taxonomy_name( $attribute ); |
|
$name = 'filter_' . $attribute; |
|
if (!empty( $_GET[$name]) && taxonomy_exists($taxonomy)) { |
|
$_chosen_attributes[$taxonomy]['terms'] = explode( ',', $_GET[$name] ); |
|
$_chosen_attributes[$taxonomy]['query_type'] = 'not'; |
|
} |
|
} |
|
} |
|
$wc_query = WC()->query; |
|
add_filter('loop_shop_post_in', 'fin_woo_layered_nav_query'); |
|
} |
|
add_action('init', 'fin_woo_layered_nav_init'); |
|
|
|
function fin_woo_layered_nav_query($filtered_posts) { |
|
global $_chosen_attributes; |
|
if (sizeof($_chosen_attributes) > 0 ) { |
|
$matched_products = array(); |
|
$tax_queries = array(); |
|
foreach ($_chosen_attributes as $attribute => $data) { |
|
$matched_products_from_attribute = array(); |
|
if (sizeof($data['terms']) > 0) { |
|
$tax_query = array( |
|
'taxonomy'=> $attribute, |
|
'terms' => $data['terms'], |
|
'field' => 'term_id', |
|
'operator' => 'NOT IN' |
|
); |
|
array_push($tax_queries, $tax_query); |
|
} |
|
} |
|
if($tax_queries) { |
|
$posts = get_posts( |
|
array( |
|
'post_type' => 'product', |
|
'numberposts' => -1, |
|
'post_status' => 'publish', |
|
'fields' => 'ids', |
|
'no_found_rows' => true, |
|
'tax_query' => $tax_queries |
|
) |
|
); |
|
} |
|
|
|
WC()->query->layered_nav_post__in = $posts; |
|
WC()->query->layered_nav_post__in[] = 0; |
|
|
|
if ( sizeof( $filtered_posts ) == 0 ) { |
|
$filtered_posts = $posts; |
|
$filtered_posts[] = 0; |
|
} else { |
|
$filtered_posts = array_intersect( $filtered_posts, $posts ); |
|
$filtered_posts[] = 0; |
|
} |
|
} |
|
return (array) $filtered_posts; |
|
} |