Created
August 1, 2019 09:03
-
-
Save Matteo182/7f19875df31edbdc2dbf355af091e35e to your computer and use it in GitHub Desktop.
Plugin per WordPress per creare filtri per prodotti di Woocommerce con custom field, genera uno short code.
This file contains 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 | |
/* | |
Plugin Name: Book Filter | |
Description: Crea uno short code per applicare i filtri alla pagina Biblioteca | |
Version: 1.0 | |
*/ | |
/* Includo js in wordpress */ | |
function enqueue_scripts(){ | |
// wp_enqueue_script('vue', 'https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js', [], '2.5.17'); | |
wp_enqueue_script('bookfilter', plugin_dir_url( __FILE__ ) . 'bookfilter.js', [], '1.0', true); | |
wp_enqueue_script('moment', 'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js', [], '2.10.6'); | |
wp_enqueue_script('bootstrap-datepicke', 'https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/js/bootstrap-datepicker.min.js', [], '1.7.1'); | |
wp_enqueue_style( 'bootstrap-datepicker-style', 'https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.css' ); | |
} | |
add_action( 'wp_enqueue_scripts', 'enqueue_scripts', 20 ); | |
function handle_shortcode() { | |
$filtri=''; | |
$filtri .= '<div id="archive-filters">'; | |
foreach( $GLOBALS["my_query_filters"] as $key => $name ): | |
$field = get_field_object($key, false, false); | |
if( isset($_GET[ $name ]) ) { | |
$field["value"] = $_GET[ $name ]; | |
} | |
if ($name != 'datauscita'){ | |
$filtri .='<label class="filter-lable" for="'.$name.'">'.$name.'</label> | |
<input type="'.$name.'" class="form-control filter" id="'.$name.'" data-filter="'.$name.'" placeholder="'.$name.'" value="'.$field["value"].'">'; | |
} | |
endforeach; | |
$filtri .='<label class="filter-lable" for="datauscita">Data Uscita</label> | |
<div class="date"> | |
<input type="text" class="form-control filter" data-filter="datauscita"> | |
<div class="input-group-addon close-button"> | |
<span class="glyphicon glyphicon-th"></span> | |
</div> | |
</div>'; | |
$filtri .='<button onclick="oraFiltra()" type="submit" class="btn btn-primary">Submit</button>'; | |
$filtri .='<button onclick="clearFiltri()" type="clear" class="btn btn-secondary">Clear</button>'; | |
$filtri .='</div>'; | |
return $filtri; | |
} | |
add_shortcode('bookFilter', 'handle_shortcode'); | |
/********************************************************** */ | |
// Hook per applicare i Filtri nella Meta Query in Prodotti | |
/********************************************************** */ | |
// array of filters (field key => field name) | |
$GLOBALS['my_query_filters'] = array( | |
'field_1' => 'autore', | |
'field_2' => 'genere', | |
'field_3' => 'editore', | |
'field_4' => 'datauscita', | |
'field_5' => 'pagine', | |
'field_6' => 'ISBN' | |
); | |
// action | |
add_action('pre_get_posts', 'my_pre_get_posts', 10, 1); | |
function my_pre_get_posts( $query ) { | |
// bail early if is in admin | |
if( is_admin() ) return; | |
// bail early if not main query | |
// - allows custom code / plugins to continue working | |
if($query->query_vars['post_type'] == 'product'){ | |
// get meta query | |
$meta_query =(array) $query->get('meta_query'); | |
// loop over filters | |
foreach( $GLOBALS['my_query_filters'] as $key => $name ) { | |
// continue if not found in url | |
if( empty($_GET[ $name ]) ) { | |
continue; | |
} | |
// get the value for this filter | |
// eg: http://www.website.com/events?city=melbourne,sydney | |
$value = explode(',', $_GET[ $name ]); | |
if ($name == 'datauscita'){ | |
$format = "d/m/Y"; | |
$dateobj = DateTime::createFromFormat($format, $value[0]); | |
$value = $dateobj->format('Ymd'); | |
} | |
// append meta query | |
$meta_query[] = array( | |
'key' => $name, | |
'value' => $value, | |
'compare' => 'IN', | |
); | |
} | |
// update meta query | |
$query->set('meta_query', $meta_query); | |
} | |
} |
This file contains 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
// Applica i filtri | |
function oraFiltra(){ | |
// vars | |
var url = window.location.origin; | |
args = {}; | |
// loop over filters | |
jQuery('#archive-filters .filter').each(function(){ | |
// vars | |
var filter = jQuery(this).data('filter'); | |
vals = jQuery(this).val(); | |
// append to args | |
args[ filter ] = vals; | |
}); | |
// update url | |
url += '?'; | |
// loop over args | |
jQuery.each(args, function( name, value ){ | |
url += name + '=' + value + '&'; | |
}); | |
// remove last & | |
url = url.slice(0, -1); | |
// reload page | |
window.location.replace( url ); | |
} | |
// Azzera tutti i filtri e ricarica la pagina | |
function clearFiltri() { | |
var url = window.location.origin; | |
// reload page | |
window.location.replace( url ); | |
} | |
//datapiker | |
/* function getUrlParameter(sParam) { | |
var sPageURL = window.location.search.substring(1), | |
sURLVariables = sPageURL.split('&'), | |
sParameterName, | |
i; | |
for (i = 0; i < sURLVariables.length; i++) { | |
sParameterName = sURLVariables[i].split('='); | |
if (sParameterName[0] === sParam) { | |
return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]); | |
} | |
} | |
}; */ | |
jQuery( document ).ready(function() { | |
jQuery('.date').datepicker({ | |
format: 'dd/mm/yyyy', | |
autoclose: true, | |
}); | |
// Unbind default events | |
jQuery('.close-button').unbind(); | |
// Per far chiudere e riaprire il Datepicker | |
jQuery('.close-button').click(function() { | |
if (jQuery('.datepicker').is(":visible")) { | |
jQuery('.date').datepicker('hide'); | |
} else { | |
jQuery('.date').datepicker('show'); | |
} | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment