Last active
May 21, 2021 22:00
-
-
Save willybahuaud/c7e131aa04898cf1de177e864e35f4c7 to your computer and use it in GitHub Desktop.
Facets filters by cookies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_shortcode( 'menu-product-filter', 'w_product_filters' ); | |
function w_product_filters() { | |
$cat = get_queried_object_id(); // si on est sur un ontenu, si c'est une archive on force la valeur (sans tirets) | |
$current = array(); | |
if ( ! empty( $_COOKIE['w_filters'] ) ) { | |
$filters = json_decode( stripslashes( $_COOKIE['w_filters'] ) ); | |
if ( ! empty( $filters->$cat ) ) { | |
$current = $filters->$cat; | |
} | |
} | |
$terms = get_terms( array( | |
'taxonomy' => 'product_category', | |
) ); | |
$product_cats = array(); | |
foreach( $terms as $t ) { | |
$product_cats[ $t->term_id ] = array( 'meta_key_name_1', $t->name ); | |
} | |
$pts = get_posts( array( | |
'post_type' => 'point_conseil', | |
'posts_per_page' => -1, | |
'suppress_filters' => false, | |
'post_status' => 'publish', | |
) ); | |
$pt_conseil = array(); | |
foreach( $pts as $pt ) { | |
$pt_conseil[ $pt->ID ] = array( 'meta_key_name_2', get_the_title( $pt ) ); | |
} | |
$out = '<div class="product-filters " id="product-filters" data-cat="' . $cat . '">'; | |
$out .= '<div class="dropdown d-inline-block"><button class="product-filters_button_list dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false"> | |
Types de produit</button> | |
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">'; | |
foreach ( $product_cats as $k => $v ) { | |
$out .= sprintf( '<li class="dropdown-item"><button class="product-filters__button%1$s" data-entitytype="%4$s" data-type="%2$s">%3$s</button></li>', | |
!empty($current->{$v[0]}) | |
&& in_array( $k, $current->{$v[0]} ) | |
? ' active' : '', $k, $v[1], $v[0] ); | |
} | |
$out .= '</ul></div>'; | |
$out .= '<div class="dropdown d-inline-block"><button class="product-filters_button_list dropdown-toggle" type="button" id="dropdownMenuButton2" data-bs-toggle="dropdown" aria-expanded="false"> | |
Points conseils</button> | |
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton2">'; | |
foreach ( $pt_conseil as $k => $v ) { | |
$out .= sprintf( '<li><button class="product-filters__button%1$s" data-entitytype="%4$s" data-type="%2$s">%3$s</button></li>', !empty($current->{$v[0]}) && in_array( $k, $current->{$v[0]} ) ? ' active' : '', $k, $v[1], $v[0] ); | |
} | |
$out .= '</ul></div>'; | |
$out .= '</div>'; | |
return $out; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'pre_get_posts', 'w_filter_query_p' ); | |
function w_filter_query_p( $q ) { | |
if ( ! is_admin() | |
&& $q->is_main_query() | |
&& ! empty( $_COOKIE['w_filters'] ) | |
&& is_post_type_archive( 'product' ) | |
) { // changer la cible ici | |
$filters = json_decode( stripslashes( $_COOKIE['w_filters'] ) ); | |
$cat = 'avis'; | |
if ( ! empty( $filters->$cat ) ) { | |
$mq = $q->get('meta_query', array() ); | |
$mq['relation'] = 'AND'; | |
foreach ( $filters->$cat as $meta_key => $values ) { | |
if ( ! empty ( $values ) ) { | |
$mq[] = array( | |
'key' => $meta_key, | |
'value' => $values, | |
'compare' => 'IN', | |
); | |
} | |
} | |
$q->set('meta_query', $mq ); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function ($) { | |
var $filters = $('#product-filters'); | |
if ( $filters.length ) { | |
var target = $filters.data('cat'); // <- ca en gros c'est pour avoir des filtres différents sur des pages différentes | |
//build filter | |
filters.on('click','button[data-entitytype]',function(e) { | |
e.preventDefault(); | |
/* commenter ca pour avoir des filtres uniques | |
$('#product-filters button.active[data-entitytype="' + entityType + '"]').not(this).removeClass('active'); | |
//*/ | |
$(this).toggleClass('active'); | |
var type = {}; | |
$.each($filters.find('button.active'), function (i, el) { | |
var entityType = $(el).data('entitytype'); | |
if ('undefined' == typeof type[entityType]) { | |
type[entityType] = []; | |
} | |
type[entityType].push($(el).data('type')); | |
}); | |
//setcookie | |
setWCookie(target, type ); | |
//ajax | |
$.ajax({ | |
method:'POST', | |
url:document.location.href, | |
success:function(data) { | |
$content = $(data).find('#content'); | |
//$found = $(data).find('#found-products'); | |
if ( ! $content.length ) { | |
$content = $('<div id="content"/>'); | |
} | |
$('#content').replaceWith($content); | |
//$('#found-products').replaceWith($found); | |
} | |
}); | |
}); | |
} | |
}); | |
//usefull | |
window.setWCookie = function ( cat, val ) { | |
var wCookie = getCookie( 'w_filters'); | |
wCookie[cat] = val; | |
console.log( wCookie ); | |
var exdays = 1, | |
cname = 'w_filters', | |
d = new Date(), | |
cvalue = JSON.stringify(wCookie); | |
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); | |
var expires = "expires=" + d.toUTCString(); | |
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; | |
} | |
window.getCookie = function (name) { | |
var match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)')); | |
if (match) return JSON.parse(match[2]); | |
return {}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment