Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FrancoStino/af16e9a7c4397cd06c0e477d0185f21e to your computer and use it in GitHub Desktop.
Save FrancoStino/af16e9a7c4397cd06c0e477d0185f21e to your computer and use it in GitHub Desktop.
Add a dropdown to filter orders by state and country into shop order list admin - Woocommerce
<?php
// Aggiungi un menu a discesa per filtrare gli ordini per stato e nazione
add_action('restrict_manage_posts', 'add_shop_order_filter_by_state_and_country');
function add_shop_order_filter_by_state_and_country(){
global $pagenow, $typenow;
if( 'shop_order' === $typenow && 'edit.php' === $pagenow ) {
// Ottieni i codici dei paesi disponibili con i codici/nomi degli stati
$nazioni = WC()->countries->get_allowed_country_states();
// Inizializzazione delle variabili
$filter_state_id = 'shipping_state';
$filter_country_id = 'shipping_country';
$current_states = ( isset($_GET[$filter_state_id]) ) ? (array)$_GET[$filter_state_id] : array();
$current_country = ( isset($_GET[$filter_country_id]) ) ? $_GET[$filter_country_id] : '';
// Output della select per la nazione
echo '<select name="shipping_country" id="shipping_country" class="wc-enhanced-select" data-placeholder="Filtra per nazione">';
echo '<option value="">' . __('Tutte le nazioni', 'woocommerce') . '</option>';
foreach($nazioni as $abbreviazione_nazione => $province){
$selected = ($current_country === $abbreviazione_nazione) ? ' selected' : '';
echo '<option value="' . $abbreviazione_nazione . '"' . $selected . '>' . WC()->countries->countries[$abbreviazione_nazione] . '</option>';
}
echo '</select>';
// Output della select per le province (vuota inizialmente)
echo '<select name="shipping_state[]" id="shipping_state" class="wc-enhanced-select" multiple data-placeholder="Filtra per provincia">';
echo '<option value="">' . __('Tutte le province', 'woocommerce') . '</option>';
echo '</select>';
// Aggiungi JavaScript per popolare dinamicamente la select delle province in base alla nazione selezionata
echo '<script>
jQuery(document).ready(function($) {
var provinces = ' . json_encode($nazioni) . ';
var $stateSelect = $("#shipping_state");
var $countrySelect = $("#shipping_country");
$countrySelect.change(function() {
var selectedCountry = $(this).val();
$stateSelect.empty(); // Cancella le opzioni esistenti
$stateSelect.append("<option value=\'\'>" + "' . __('Tutte le province', 'woocommerce') . '" + "</option>");
if (selectedCountry && provinces[selectedCountry]) {
$.each(provinces[selectedCountry], function(abbreviazione_provincia, provincia) {
$stateSelect.append("<option value=\'" + abbreviazione_provincia + "\'>" + provincia + "</option>");
});
}
});
// Trigger evento di cambio al caricamento della pagina se una nazione è preselezionata
if ("' . $current_country . '" !== "") {
$countrySelect.trigger("change");
}
});
</script>';
}
}
// Process the filter dropdown for orders by shipping state and country
add_filter( 'request', 'process_admin_shop_order_filtering_by_state_and_country', 99 );
function process_admin_shop_order_filtering_by_state_and_country( $vars ) {
global $pagenow, $typenow;
if ( $pagenow == 'edit.php' && 'shop_order' === $typenow) {
$filter_state_id = $_GET['shipping_state'] ?? null;
$filter_country_id = $_GET['shipping_country'] ?? null;
if ( isset( $filter_state_id ) && ! empty($filter_state_id) ) {
$vars['meta_key'] = '_billing_state';
$vars['meta_value'] = $filter_state_id;
$vars['orderby'] = 'meta_value';
}
if ( isset( $filter_country_id ) && ! empty($filter_country_id) ) {
$vars['meta_key'] = '_billing_country';
$vars['meta_value'] = $filter_country_id;
}
}
return $vars;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment